oauth2client Credentials refresh_token becomes null
Asked Answered
O

1

5

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!

Obloquy answered 14/8, 2014 at 8:11 Comment(0)
C
7

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

Chevaldefrise answered 22/9, 2014 at 6:17 Comment(3)
What does it mean when Google says: "By default, our client libraries automatically refresh expired access tokens." ? the refresh method should be called manually, shouldn't it ?Vladi
No, I think it is called automatically for you by the libraryChevaldefrise
And what about refresh tokens? can they be expired?Vladi

© 2022 - 2024 — McMap. All rights reserved.