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;
Shan Khan
  • 9,667
  • 17
  • 61
  • 111

2 Answers2

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
    });
SaguiItay
  • 2,145
  • 1
  • 18
  • 40
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.

Community
  • 1
  • 1
peleyal
  • 3,472
  • 1
  • 14
  • 25
  • what value to set to refresh token ? it will be the latest token received ? – Shan Khan Nov 23 '15 at 11:42
  • 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, https://github.com/google/google-api-dotnet-client/blob/master/Src/GoogleApis.Auth/OAuth2/AuthorizationCodeInstalledApp.cs#L67 & https://github.com/google/google-api-dotnet-client/blob/master/Src/GoogleApis.Auth/OAuth2/Responses/TokenResponse.cs#L66 – peleyal Nov 24 '15 at 13:52