Chr*_*riz 15 python google-authentication google-sheets pandas google-colaboratory
我是 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.
Run Code Online (Sandbox Code Playgroud)
这是我用来创建身份验证的代码。
#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())
Run Code Online (Sandbox Code Playgroud)
任何帮助将非常感激。
小智 45
我今天遇到了同样的问题,找到了这个答案: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)
Run Code Online (Sandbox Code Playgroud)