1

I want to use same OAuth Code and Token for Both sheet and google drive api without redirecting to page 2 times for sheet and drive.

Following is the code for generating the access code and token using oauth 2

                string SCOPE = "https://spreadsheets.google.com/feeds https://docs.google.com/feeds";
                OAuth2Parameters parameters = new OAuth2Parameters();
                parameters.ClientId = CLIENT_ID;
                parameters.ClientSecret = CLIENT_SECRET;
                parameters.RedirectUri = REDIRECT_URI;
                parameters.Scope = SCOPE;
                string authorizationUrl = OAuthUtil.CreateOAuth2AuthorizationUrl(parameters); 
                OAuthUtil.GetAccessToken(parameters);
                string accessToken = parameters.AccessToken;

As in asp.net mvc its been generated through redirection for google sheet API.

I want to use google drive API for this same token also.

How i can create credential object for google Drive API in order to work with that. Following CODE OPENS ANOTHER WINDOW AND SEND CODE TO THAT WINDOW.

var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets
      {
         ClientId = clientId,
         ClientSecret = clientSecret
      }, scopes, "skhan", CancellationToken.None, new FileDataStore("Drive.Auth.Store")).Result;

2 Answers 2

5

You can use the code below, that assumes that you already have Access/Refresh tokens:

var flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
    {
        ClientSecrets = new ClientSecrets
            {
                ClientId = ClientId,
                ClientSecret = ClientSecret
            },
            Scopes = new[] { DriveService.Scope.Drive }
    });

var credential = new UserCredential(_flow, UserIdentifier, new TokenResponse
    {
        AccessToken = AccessToken,
        RefreshToken = RefreshToken
    });
var service = new DriveService(new BaseClientService.Initializer
    {
        ApplicationName = "MyApp",
        HttpClientInitializer = credential,
        DefaultExponentialBackOffPolicy = ExponentialBackOffPolicy.Exception | ExponentialBackOffPolicy.UnsuccessfulResponse503
    });
Sign up to request clarification or add additional context in comments.

Comments

0

You can use the approach mentioned here: .NET Google api 1.7 beta authenticating with refresh token, just set on the token response your access and refersh tokens and you are ready to go.

2 Comments

what value to set to refresh token ? it will be the latest token received ?
If you don't have refresh token, don't set it. If you have access token it's valid for only one our, so you have to set the token time correctly, github.com/google/google-api-dotnet-client/blob/master/Src/… & github.com/google/google-api-dotnet-client/blob/master/Src/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.