0

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?

riders994
  • 1,246
  • 5
  • 13
  • 33

1 Answers1

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 use access_token.
  • refresh token

If this was not useful for you, I'm sorry.

Tanaike
  • 181,128
  • 11
  • 97
  • 165