2

I'm using oAuth 2.0 to authenticate gmail api v1 and getting the access token and refresh token but how to get access token using refresh token.

UserCredential credential;
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
       new ClientSecrets
       {
          ClientId = "my client id",
          ClientSecret = "my client secret"
       },
       new[] { GmailService.Scope.GmailSend },
       "user",
       CancellationToken.None).Result;

// Create Gmail API service.
var service = new GmailService(new BaseClientService.Initializer()
{
   HttpClientInitializer = credential,
   ApplicationName = ApplicationName,
});

string RefreshToken = credential.Token.RefreshToken;

3 Answers 3

1

The Google .Net client library handles all that for you. Once you make your first request using your service it will request an access token using the refresh token.

Anwser: make a simple request to the Gmail API will fetch a access token if needed.

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

Comments

0

I don't know how to do it using .Net library. To do it manually,

POST

"client_secret=" + [ClientSecret] + "&grant_type=refresh_token" + "&refresh_token=" + [RefreshToken] + "&client_id=" + [ClientID]

To https://www.googleapis.com/oauth2/v3/token

You need to replace data in square bracket with your details

Comments

0

working code

string[] scopes = new string[] { "https://mail.google.com/" };

UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
         new ClientSecrets
         {
            ClientId = "my client id",
            ClientSecret = "my client secret"
         },
         scopes,
        "user",
         CancellationToken.None).Result;
string RefreshToken = credential.Token.RefreshToken;//you will get Refreshtoken

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

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.