'How to get access token from refresh token in gmail api in asp.net
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;
Solution 1:[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.
Solution 2:[2]
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
Solution 3:[3]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | DaImTo |
Solution 2 | lal |
Solution 3 |