Search code examples
pythongoogle-colaboratorygoogle-workspace

AuthorizationError: Failed to fetch user credentials in google Colab


I have a colab notebook stored in a shared google drive. The idea is to share this notebook with other gsuite users. When they copy-paste it in their local drive, some of them can run it and work on it, while some others get the above error printed.

The code that gives the error is the following:

!pip install -U -q PyDrive
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials
# Authenticate and create the PyDrive client.
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)

This part of the code prints the following error:

AuthorizationError                        Traceback (most recent call last)
<ipython-input-9-6beb6b456bb8> in <module>()
      5 from oauth2client.client import GoogleCredentials
      6 # Authenticate and create the PyDrive client.
----> 7 auth.authenticate_user()
      8 gauth = GoogleAuth()
      9 gauth.credentials = GoogleCredentials.get_application_default()

/usr/local/lib/python3.6/dist-packages/google/colab/auth.py in authenticate_user(clear_output)
    160   if _check_adc():
    161     return
--> 162   raise _errors.AuthorizationError('Failed to fetch user credentials')

AuthorizationError: Failed to fetch user credentials

I am new to colab and gsuite in general and I am not sure how to proceed. Any help is welcome


Solution

  • Apparently the issue was related to the previous step. After executing

    !pip install -U tensorflow-gpu==2.0.0 grpcio
    

    It was necessary to restart the runtime, i.e.:

    enter image description here

    I am not 100% sure about the reason for this, but it solved the problem. I hope it helps!