sohaib javed
sohaib javed

Reputation: 179

Google Drive api .Net Client Simple Authorization with existing access token

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.

Upvotes: 0

Views: 936

Answers (1)

Serhii Kyslyi
Serhii Kyslyi

Reputation: 1823

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.

Upvotes: 2

Related Questions