Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Google api 1.7 beta authenticating with refresh token

I've been looking at the Oauth .Net Google Apis in order to authenticate via OAuth and use the Google drive Apis.

Specifically, I want to use a refresh token I already have stored in order to use it to instantiate a GoogleDrive service.

I've found samples like https://code.google.com/p/google-api-dotnet-client/source/browse/Tasks.SimpleOAuth2/Program.cs?repo=samples

That seem to use "GoogleWebAuthorizationBroker.AuthorizeAsync" but I'm not sure how I can use that method with a refresh token rather than the client secrets you seem to be feeding it in this example.

like image 755
Dan G Avatar asked Jan 08 '14 22:01

Dan G


1 Answers

If I understand you correctly, you are asking how can you create a new Google service, based on an existing refresh token.

So, you can do the following:

var token = new TokenResponse { RefreshToken = "YOUR_REFRESH_TOKEN_HERE" }; 
var credentials = new UserCredential(new GoogleAuthorizationCodeFlow(
    new GoogleAuthorizationCodeFlow.Initializer 
    {
      ClientSecrets = [your_client_secrets_here]
    }), "user", token);

Then you can pass your credentials to the service's initializer.

By doing the above, GoogleAuthorizationCodeFlow will get a new access token based on you refresh token and client secrets.

Note that you must have client secrets here, without that, you won't be able to get an access token.

like image 181
peleyal Avatar answered Sep 20 '22 17:09

peleyal