I have completed steps of authorization and obtained access token and refresh token.
What should I do next to generate access token using refresh token that I have s
If you using Java then follow below code snippet :
GoogleCredential refreshTokenCredential = new GoogleCredential.Builder().setJsonFactory(JSON_FACTORY).setTransport(HTTP_TRANSPORT).setClientSecrets(CLIENT_ID, CLIENT_SECRET).build().setRefreshToken(yourOldToken);
refreshTokenCredential.refreshToken(); //do not forget to call this
String newAccessToken = refreshTokenCredential.getAccessToken();
If you want to implement that yourself, the OAuth 2.0 flow for Web Server Applications is documented at https://developers.google.com/accounts/docs/OAuth2WebServer, in particular you should check the section about using a refresh token:
https://developers.google.com/accounts/docs/OAuth2WebServer#refresh
All you need to do is a post request like below :-
POST https://www.googleapis.com/oauth2/v4/token
Content-Type: application/json
{
"client_id": <client_id>,
"client_secret": <client_secret>,
"refresh_token": <refresh_token>,
"grant_type": "refresh_token"
}
It's an old question but seems to me it wasn't completely answered, and I needed this information too so I'll post my answer.
If you want to use the Google Api Client Library, then you just need to have an access token that includes the refresh token in it, and then - even though the access token will expire after an hour - the library will refresh the token for you automatically.
In order to get an access token with a refresh token, you just need to ask for the offline access type (for example in PHP: $client->setAccessType("offline");
) and you will get it. Just keep in mind you will get the access token with the refresh token only in the first authorization, so make sure to save that access token in the first time, and you will be able to use it anytime.
Hope that helps anyone :-)
Just posting my answer in case it helps anyone as I spent an hour to figure it out :)
First of all two very helpful link related to google api and fetching data from any of google services:
https://developers.google.com/analytics/devguides/config/mgmt/v3/quickstart/web-php
https://developers.google.com/identity/protocols/OAuth2WebServer
Furthermore, when using the following method:
$client->setAccessToken($token)
The $token
needs to be the full object returned by the google when making authorization request, not the only access_token
which you get inside the object so if you get the object lets say:
{"access_token":"xyz","token_type":"Bearer","expires_in":3600,"refresh_token":"mno","created":1532363626}
then you need to give:
$client->setAccessToken('{"access_token":"xyz","token_type":"Bearer","expires_in":3600,"refresh_token":"mno","created":1532363626}')
Not
$client->setAccessToken('xyz')
And then even if your access_token
is expired, google will refresh it itself by using the refresh_token
in the access_token
object.
If you are using web api then you should make a http POST
call to URL : https://www.googleapis.com/oauth2/v4/token
with following request body
client_id: <YOUR_CLIENT_ID>
client_secret: <YOUR_CLIENT_SECRET>
refresh_token: <REFRESH_TOKEN_FOR_THE_USER>
grant_type: refresh_token
refresh token never expires so you can use it any number of times. The response will be a JSON like this:
{
"access_token": "your refreshed access token",
"expires_in": 3599,
"scope": "Set of scope which you have given",
"token_type": "Bearer"
}