0

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.

2
  • 3
    Is 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. Commented Jul 9, 2017 at 20:25
  • Yes its the same problem, I am trying workaround code. Thanks Commented Jul 9, 2017 at 20:38

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.