sanvlr12 / google-api-dotnet-client

Automatically exported from code.google.com/p/google-api-dotnet-client
0 stars 0 forks source link

Sample for using RefreshTokens to avoid having to request access on every application launch #143

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Which Google API and version (e.g. Google Tasks API version 1)?
Google+ API v1

What should the sample demonstrate (e.g. using OAuth2, using a specific
feature of that API)?
Using OAuth2 in a way that we don't have to request access every time 
(RefreshToken?). Everything I try gives "400 Bad Request" errors.

Platform (e.g. Windows, Mono, Silverlight, WP7)?
Windows, .NET

External references, such as API reference guide?

Please provide any additional information below.

The below code works, but I want to be able to store a token and reuse it 
later. I've tried many things using the RefreshTokens, but always get errors:

class Program
    {
        private const string Scope = "https://www.googleapis.com/auth/plus.me";

        static void Main(string[] args)
        {
            var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description);
            provider.ClientIdentifier = "BLAH";
            provider.ClientSecret = "BLAH";
            var auth = new OAuth2Authenticator<NativeApplicationClient>(provider, GetAuthentication);

            var plus = new PlusService(auth);
            plus.Key = "BLAH";
            var me = plus.People.Get("me").Fetch();
            Console.WriteLine(me.DisplayName);
        }

        private static IAuthorizationState GetAuthentication(NativeApplicationClient arg)
        {
            // Get the auth URL:
            IAuthorizationState state = new AuthorizationState(new[] { Scope });
            state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl);
            Uri authUri = arg.RequestUserAuthorization(state);

            // Request authorization from the user (by opening a browser window):
            Process.Start(authUri.ToString());
            Console.Write("  Authorization Code: ");
            string authCode = Console.ReadLine();
            Console.WriteLine();

            // Retrieve the access token by using the authorization code:
            return arg.ProcessUserAuthorization(authCode, state);
        }
    }

Original issue reported on code.google.com by danny.tuppeny on 17 Sep 2011 at 12:54

GoogleCodeExporter commented 9 years ago
Hi, and thanks for the report! There should definitely a sample to use the 
Plus-API with advanced OAuth2 authentication.

For the moment I recommend looking at a sample which uses the Windows Protected 
Data to cache a refresh token:

  http://code.google.com/p/google-api-dotnet-client/source/browse/Tasks.CreateTasks/Program.cs?repo=samples

In this sample you can also find some nicer ways of requesting an Access Code 
from the user. Maybe that is of some help to you.

Original comment by mlinder...@gmail.com on 17 Sep 2011 at 9:01

GoogleCodeExporter commented 9 years ago

Original comment by asky...@google.com on 27 Oct 2011 at 4:24

GoogleCodeExporter commented 9 years ago

Original comment by asky...@google.com on 25 Apr 2012 at 5:56