2021-05-17 06:27:26
To refresh an Access Token, you call the Google OAuth endpoint passing in three parameters:
Client ID
Client Secret
Refresh Token
This can be done very simply with a simple HTTP POST request.
Here is an example using curl:
set REFRESH_TOKEN=REPLACE_WITH_REFRESH_TOKEN
curl ^
--data client_id=%CLIENT_ID% ^
--data client_secret=%CLIENT_SECRET% ^
--data grant_type=refresh_token ^
--data refresh_token=%REFRESH_TOKEN% ^
https://www.googleapis.com/oauth2/v4/token
In Python using the requests library:
// Call refreshToken which creates a new Access Token
access_token = refreshToken(client_id, client_secret, refresh_token)
// Pass the new Access Token to Credentials() to create new credentials
credentials = google.oauth2.credentials.Credentials(access_token)
// This function creates a new Access Token using the Refresh Token
// and also refreshes the ID Token (see comment below).
def refreshToken(client_id, client_secret, refresh_token):
params = {
"grant_type": "refresh_token",
"client_id": client_id,
"client_secret": client_secret,
"refresh_token": refresh_token
}
authorization_url = "https://www.googleapis.com/oauth2/v4/token"
r = requests.post(authorization_url, data=params)
if r.ok:
return r.json()['access_token']
else:
return None
Note: This code will also return a refreshed ID Token if originally requested during the authorization request.
9 views03:27