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
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.
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;
}`