Chriz
Chriz

Reputation: 231

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

I'm new to python and currently is working on a project that requires me to export pandas data frame from google collab to a google spreadsheet with multiple tabs. Previously when I run this specific code, there are no errors but then now it shows an error like this:

    TypeError                                 Traceback (most recent call last)
<ipython-input-74-c8b829c43616> in <module>()
      5 gauth.credentials = GoogleCredentials.get_application_default()
      6 drive = GoogleDrive(gauth)
----> 7 gc = gspread.authorize(GoogleCredentials.get_application_default())

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

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

Here is the code that I use to create authentication.

#Import PyDrive and associated libraries.
#This only needs to be done once per notebook.

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials
import gspread

#Authenticate and create the PyDrive client.
#This only needs to be done once per notebook.
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)
gc = gspread.authorize(GoogleCredentials.get_application_default())

Any help would be much appreciated.

Upvotes: 17

Views: 15407

Answers (1)

Dj Melmac
Dj Melmac

Reputation: 486

I had the same problem today and found this answer: https://github.com/burnash/gspread/issues/1014#issuecomment-1082536016

I finally solved it by replacing the old code with this one:

from google.colab import auth
auth.authenticate_user()

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

gc = gspread.authorize(creds)

Upvotes: 47

Related Questions