0

At the moment my code does not automatically refresh my access code when it expires. I'm at a loss at how to implement the code from this post (How to generate access token using refresh token through Google Drive SDK in .NET?) inside my own code.

My code:

    private static IAuthorizationState GetAuthorization(NativeApplicationClient arg)
    {
        // Get the auth URL:
        IAuthorizationState state = new AuthorizationState(new[] { "https://www.googleapis.com/auth/yt-analytics.readonly" });
        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);
    }

Specifically I don't get this line. How to save my refresh token and insert it in state.RefreshToken? I assume I need to cram those 2 lines (state.RefreshToken and arg.RefreshToken(state)) just before return state?

state.RefreshToken = "<refresh token previously saved>";
Community
  • 1
  • 1
tutu
  • 673
  • 2
  • 13
  • 31

1 Answers1

-2

Here's an example that stores OAuth 2 credentials in a local file and automatically generates a new access token from the refresh token before each invocation: https://developers.google.com/youtube/v3/code_samples/dotnet#my_uploads

The access token should last for one hour. If your code runs continuously and you need to make an API call after an hour has passed, you can explicitly call the GetAuthorization() method again.

Jeff Posnick
  • 53,580
  • 14
  • 141
  • 167
  • I'm getting an error for AuthorizationMgr, "The name 'AuthorizationMgr' does not exist in the current context". – tutu Jun 25 '13 at 10:02
  • Make sure you have all the same `using ...` imports as in the example, and that you have included the DLLs in your project. There's a comment at the top of the example with links to download all the dependencies. – Jeff Posnick Jun 25 '13 at 14:53
  • Still does not work somehow. But I got it working with this example: http://stackoverflow.com/questions/7454930/google-api-how-can-i-use-refreshtokens-to-avoid-requesting-access-every-time-m. Thanks. – tutu Jun 26 '13 at 06:52