I'm writing a script that periodically adds data to a google sheet using the API. Google recommends using the oauth2client python package for this process. Since I want to leave this script running for a while, is there a way to verify that the authentication is still valid, and if not, re-authenticate?
Asked
Active
Viewed 145 times
0
-
Does the authentication you say mean the access token? You want to know the expilation time of access token, and know how to update the access token. Is my understanding correct? – Tanaike Nov 29 '17 at 22:41
-
Yeah, @Tanaike, pretty much. – riders994 Nov 30 '17 at 15:29
-
Thank you for your reply. I posted my answer. Please confirm it. – Tanaike Nov 30 '17 at 22:39
-
Was my answer useful for you? – Tanaike Dec 05 '17 at 05:45
1 Answers
0
How about this answer?
1. How to retrieve expiration time
In order to retrieve the expiration time of access token, you can use tokeninfo which is one of Google APIs.
Endpoint :
GET https://www.googleapis.com/oauth2/v3/tokeninfo?access_token=### access token ###
curl sample :
curl -L https://www.googleapis.com/oauth2/v3/tokeninfo?access_token=### access token ###
You are not required to authorize to use this.
Response :
{
"azp": "#####",
"aud": "#####",
"scope": "#####",
"exp": "1234567890",
"expires_in": "3600",
"access_type": "offline"
}
You can retrieve the expiration time.
exp
means the expiration time of access token.expires_in
means the remaining time of access token.
2. How to update the access token
You can retrieve new access token using refresh token. The expiration time is reset. When you retrieve access token, you already have the refresh token.
If you have it, you can use it. If you have no refresh token, you can see how to retrieve it at here. I remembered that I had answered about this before.
References :
- tokeninfo
- In this document, the query is
id_token
. In the case of access token, please useaccess_token
.
- In this document, the query is
- refresh token
If this was not useful for you, I'm sorry.

Tanaike
- 181,128
- 11
- 97
- 165