Is there any way to use .Net Api client with existing valid access token. I want to use google drive api v3 client with existing access token but I am finding no way to fill UserCredential object. In this case I am unable to use .Net Client so have to run all operations using httpClient.
-
3Is this the same problem you're facing? github.com/google/google-api-dotnet-client/issues/761. There's some workaround code in there. You could probably implement the initializer directly just to insert the token as a simpler approach though.Jon Skeet– Jon Skeet2017-07-09 20:25:01 +00:00Commented Jul 9, 2017 at 20:25
-
Yes its the same problem, I am trying workaround code. Thankssohaib javed– sohaib javed2017-07-09 20:38:05 +00:00Commented Jul 9, 2017 at 20:38
Add a comment
|
1 Answer
I have faced similar problem, but immediately found out it. You could use something similar to the following code, but slightly modified, depending on your requirements.
var flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = new ClientSecrets
{
ClientId = "your_client_id",
ClientSecret = "your_client_secret_if_necessary"
},
Scopes = new [] { "scope_you_wish_to_access"}
});
var credential = new UserCredential(flow, "user_id", new TokenResponse
{
AccessToken = "user_access_token",
RefreshToken = "user_refresh_token_if_necessary"
// additional parameters if necessary
});
var service = new Any_Google_Service(new BaseClientService.Initializer
{
HttpClientInitializer = credential
});
Google lib will automatically do a lot of stuff under hood, e.g. refreshing of expired access tokens. Hope it helps you.