Offline refresh token using Google APIs

1.1k views Asked by At

I'm trying to use Google Analytics API and have access while the user is offline. I use the oauth2decorator_from_clientsecrets to get the credentials initially(which should request offline access by default). I am able to access the API for 1 hour before the token expires as expected. After this, I end up getting an error of:

"AccessTokenRefreshError: invalid_grant"

I am using the following code to access the API with stored credentials:

credentials = StorageByKeyName(CredentialsModel, user_id, 'credentials').get()
http = httplib2.Http()
http = credentials.authorize(http)
analytics = build("analytics", "v3", http=http)
query = analytics.management().webproperties().list(accountId="123456").execute()

I suspect this has something to do with not getting initial offline access permission properly. When the user is initially directed to the authorization url, I cannot see the expected confirmation of

"Perform these operations when I'm not using the application"

Instead, what I see is:

confirmation url

Also, I am using the "Client ID for web applications" for my application.

Cheers!

1

There are 1 answers

1
AshClarke On BEST ANSWER

I'm not happy about spending all day on this issue, but here's the solution I had.

I revoked the application I had made from the user, then I deleted the credentials stored from my database.

I'm sure I'd already done that as a first step, but apparently not.

There's 10 hours of my life I won't get back. Hope this is useful to someone.