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

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

我是 python 的新手,目前正在开展一个项目,该项目要求我将 pandas 数据框从 google collab 导出到 google 电子表格多个选项卡。以前当我 运行 这个特定的代码时,没有错误但是现在它显示这样的错误:

    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.

这是我用来创建身份验证的代码。

#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())

如有任何帮助,我们将不胜感激。

我今天遇到了同样的问题,找到了这个答案:https://github.com/burnash/gspread/issues/1014#issuecomment-1082536016

我终于用这个替换旧代码解决了这个问题:

from google.colab import auth
auth.authenticate_user()

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

gc = gspread.authorize(creds)