Getting AccessTokenRefreshError: invalid_grant in Google API fro service account

2.5k Views Asked by At

I am following this example

https://code.google.com/p/google-api-python-client/source/browse/samples/service_account/tasks.py

  credentials = SignedJwtAssertionCredentials(
      '[email protected]',
      key,
      scope='https://www.googleapis.com/auth/tasks')
  http = httplib2.Http()
  http = credentials.authorize(http)

  service = build("tasks", "v1", http=http)

  # List all the tasklists for the account.
  lists = service.tasklists().list().execute(http=http)
  pprint.pprint(lists)

The issue is , it works sometimes and i get the lists as JSON and after running program few more times i get error

  File "/usr/local/lib/python2.7/site-packages/oauth2client/client.py", line 710, in _do_refresh_request
    raise AccessTokenRefreshError(error_msg)
oauth2client.client.AccessTokenRefreshError: invalid_grant
3

There are 3 best solutions below

0
On

I'm interface Google Drive but found the same error. In Issue 160 there is a report of setting the appropriate time on your local computer. Ever since I upgraded to Mac Mavericks I found that the I need to keep updating my system time. I was getting your reported error, set my system time back to current and I eliminated the error.

0
On

Oauth service is highly dependent on the time, make sure your client uses NTP or another time syncing mechanism.

Test with curl -I https://docs.google.com/ ; date -u

You should see the same Date: (or whithin a few seconds) for this to work

0
On

Are you running this code in a VM or sandboxed environment? If so, it could just be that your VM's clock isn't synchronised to your host machine. See a response to a similar question here.

I suffered the same (frustrating) problem and found that simply restarting my VM (ensuring the time was synchronised to the host machine (or at least set to the local timezone) fixed the problem.