Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

oauth2client Credentials refresh_token becomes null

Backgound

  1. I got access_token to Google API using the google-api-python-client django_sample.
  2. To have offline access, I've added FLOW.params['access_type'] = 'offline'.
  3. Stored credentials_json = credentials.to_json(). It contains a refresh_token.
  4. Restored the credentials Credentials.new_from_json(credentials_json).
  5. Used this credentials to gain access by credentials.authorize(http).
  6. Worked perfectly =)

The problem

  1. I did the the same every 5 minutes.
  2. In each iteration I stored the credentials and printed it.
  3. After 1 hour and 45 minutes, the "refresh_token" became null.
  4. At this point the code stopped working =(

My questions

  1. Does Credentials class refresh it's token automatically?
  2. If not, at what point of should I call credentials.refresh(http)?

Thanks!

like image 949
Michael Avatar asked Aug 14 '14 08:08

Michael


1 Answers

Your refresh token is used to get a new access token every time that the access token expires.

Here google says that the access token is automatically refreshed using the refresh token when it expires.

In our application, we call credentials.refresh(http) when the token is near to expiry

if (credentials.token_expiry - datetime.utcnow()) < timedelta(minutes=refresh_mins): credentials.refresh(httplib2.Http())

refresh_mins has a default value of 15 in our code base. This is because the access token expires in 60 minutes. We refresh every 45 mins. More details about this can be found here

like image 141
Devin Avatar answered Oct 18 '22 14:10

Devin