How to generate access token using refresh token through Google Drive SDK in .NET?

前端 未结 2 912

I have a .NET application that is using Google Drive to access the user\'s file. I am able to get the authorization code, and I have been able to exchange the authorization

相关标签:
2条回答
  • 2021-01-05 04:06

    If you are using the .NET client library (http://code.google.com/p/google-api-dotnet-client/), you don't have to take care of that. The library will automatically request a new access token for you when needed using the refresh token you retrieved the first time.

    0 讨论(0)
  • 2021-01-05 04:25

    I studied a more suitable sample: the Tasks.WinForms.NoteMgr of the GoogleApisSample... and with it I found the solution.

    The solution is in the code below. The key part of it is calling arg.RefreshToken(state);

    Thanks.

        public static Authentication.IAuthenticator UseSavedAuthorization()
        {          
    
            var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description);
            provider.ClientIdentifier = ClientID;
            provider.ClientSecret = ClientSecret;
    
            OAuth2Authenticator<NativeApplicationClient> auth = new OAuth2Authenticator<NativeApplicationClient>(provider, getState);
    
            auth.LoadAccessToken();
    
            return auth;             
        }
    
    
    public static IAuthorizationState getState(NativeApplicationClient arg)
        {
            IAuthorizationState state = new AuthorizationState(new[] { TasksService.Scopes.Tasks.GetStringValue(), 
                    DriveService.Scopes.DriveFile.GetStringValue() , DriveService.Scopes.Drive.GetStringValue()
            });
            state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl);
    
            state.RefreshToken = "<refresh token previously saved>";        
            arg.RefreshToken(state); 
    
            return state; 
        }`
    
    0 讨论(0)
提交回复
热议问题