Search code examples
pythongoogle-app-enginegoogle-cloud-storage

Google Cloud Storage HttpAccessTokenRefreshError: invalid_grant: Bad Request


I am receiving this error oauth2client.client.HttpAccessTokenRefreshError: invalid_grant: Bad Request when I try to connect to a Google Cloud Storage bucket in any of my projects (all of which were working previously).

from gcloud import storage
client = storage.Client() 
bucket = client.get_bucket(bucket_name)

I signed into my gcloud account through command line on a co-worker's laptop and the script connected to Cloud Storage fine. My co-worker then signed into his gcloud account on my computer and it did not work for him either. We assumed it was the server time issue that many other posts reference or something very wrong with my computer.

We ran ntpdate and got 8 Nov 12:00:38 ntpdate[3468]: no servers can be used, exiting we also went through using ntpd but to no avail.

Project in       : Python 2.7 | 
OS               : MacOS Sierra v 10.12.1 |
Google cloud sdk : 133.0.0

Solution

  • I had checked my authentication with gcloud and had revoked and logged back in using these commands

    gcloud auth revoke and gcloud auth login and then following the steps that come after that to get the authentication. These steps did not work.

    I then dug through Google's not so friendly documentation for hours and got back to this page: https://googlecloudplatform.github.io/google-cloud-python/stable/google-cloud-auth.html

    If you're developing locally, the easiest way to authenticate is using the Google Cloud SDK:

    $ gcloud beta auth application-default login

    Note that this command generates credentials for client libraries. To authenticate the CLI itself, use:

    $ gcloud auth login

    I have yet to finish readying what the difference between these two commands are but all of my projects now work again. Good luck

    IN SHORT: use gcloud beta auth application-default login