Reputation: 10319
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;
Upvotes: 1
Views: 2301
Reputation: 2175
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
});
Upvotes: 5
Reputation: 3512
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.
Upvotes: 0