'Gspread Not Working with Google Colab "Authenticate User" and Oauth2Client

In Google Colab, when using the example below, I am now getting an error. This worked for years, it stopped working today for me.

When utilizing the example:

Cell 1

from google.colab import auth
auth.authenticate_user()

Cell 2

import gspread
from oauth2client.client import GoogleCredentials

gc = gspread.authorize(GoogleCredentials.get_application_default())`

Error:


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-ac9436b5eeee> in <module>()
      2 from oauth2client.client import GoogleCredentials
      3 
----> 4 gc = gspread.authorize(GoogleCredentials.get_application_default())


/usr/local/lib/python3.7/dist-packages/gspread/__init__.py in authorize(credentials, client_class)
     38     """
     39 
---> 40     client = client_class(auth=credentials)
     41     return client

/usr/local/lib/python3.7/dist-packages/gspread/client.py in __init__(self, auth, session)
     38     def __init__(self, auth, session=None):
     39         if auth is not None:
---> 40             self.auth = convert_credentials(auth)
     41             self.session = session or AuthorizedSession(self.auth)
     42         else:

/usr/local/lib/python3.7/dist-packages/gspread/utils.py in convert_credentials(credentials)
     57 
     58     raise TypeError(
---> 59         "Credentials need to be from either oauth2client or from google-auth."
     60     )
     61 

TypeError: Credentials need to be from either oauth2client or from google-auth.

I don't know where to go from here. I reached out to Google Cloud support, but realized this was out of their scope. The authentication flow is working from a Google standpoint.



Solution 1:[1]

Google colab has recently changed their API. The old API has been deprecated and a new one is made available.

Here is a the new code snipet that is provided by Google and works fine with gspread

from google.colab import auth
auth.authenticate_user()

import gspread
from google.auth import default
creds, _ = default()

gc = gspread.authorize(creds)

sh = gc.create('A new spreadsheet')

Solution 2:[2]

I had to restart the runtime and follow a new protocol. I think what we were using before is now deprecated...

https://colab.research.google.com/notebooks/snippets/sheets.ipynb#scrollTo=6d0xJz3VzLOo

Solution 3:[3]

You should change the authentication method following this guide

  1. Head into Google developers console

  2. In the box labeled “Search for APIs and Services”, search for "Google Drive API" and "Google Sheets" and enable it

  3. Go to “APIs & Services > Credentials” and choose “Create credentials > Service account key”.

  4. Fill out the form

  5. Click “Create” and “Done”.

  6. Press “Manage service accounts” above Service Accounts.

  7. Press on ? near recently created service account and select “Manage keys” and then click on “ADD KEY > Create new key”.

  8. Select JSON key type and press “Create”.

You're going to get a json like this:

{
    "type": "service_account",
    "project_id": "api-project-XXX",
    "private_key_id": "2cd … ba4",
    "private_key": "-----BEGIN PRIVATE KEY-----\nNrDyLw … jINQh/9\n-----END PRIVATE KEY-----\n",
    "client_email": "[email protected]",
    "client_id": "473 … hd.apps.googleusercontent.com",
    ...
}
  1. Go to your spreadsheet and share it with a client_email from the step above. Just like you do with any other Google account.

  2. Put the json file inside a folder in your drive

  3. Change your cells to:

import gspread

json_dir = 'drive/MyDrive/credentials/credentials.json'

gc = gspread.service_account(filename=json_dir)

where json_dir is the path to json file created in step 8

Hope it helps

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Lavigne958
Solution 2 Amy Krystosik
Solution 3 Lucas Mucidas