Gspread 无法与 Google Colab“验证用户”一起使用和 Oauth2Client
在Google Colab中,使用下面的示例时,我现在会遇到错误。多年来,它今天停止了工作。
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.我接触了Google Cloud的支持,但意识到这超出了他们的范围。身份验证流从Google的角度工作。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Google Colab最近改变了其API。旧的API已被弃用,并提供了新的API。
这是由Google提供的新代码狙击
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
您应该更改以下的身份验证方法,以下本指南
在标有“搜索APIS和Services”的框中,搜索对于“ Google Drive API”和“ Google Sheets”,并启用它
转到“ Apis&amp;服务&GT;凭证”,然后选择“创建凭据&gt;服务帐户密钥”。
填写表格
单击“创建”和“完成”。
按服务帐户上方的“管理服务帐户”。
按⋮最近创建的服务帐户,然后选择“管理密钥”,然后单击“添加键&gt;创建新密钥”。
选择JSON键类型,然后按“创建”。
您将获得这样的JSON:
转到您的电子表格,并在上面的步骤中与
client_email
共享。就像您使用其他任何Google帐户一样。将JSON文件放入驱动器中的文件夹中
将您的单元格更改为:
where
json_dir
是步骤8中创建的JSON文件的路径,希望它有帮助
You should change the authentication method following this guide
Head into Google developers console
In the box labeled “Search for APIs and Services”, search for "Google Drive API" and "Google Sheets" and enable it
Go to “APIs & Services > Credentials” and choose “Create credentials > Service account key”.
Fill out the form
Click “Create” and “Done”.
Press “Manage service accounts” above Service Accounts.
Press on ⋮ near recently created service account and select “Manage keys” and then click on “ADD KEY > Create new key”.
Select JSON key type and press “Create”.
You're going to get a json like this:
Go to your spreadsheet and share it with a
client_email
from the step above. Just like you do with any other Google account.Put the json file inside a folder in your drive
Change your cells to:
where
json_dir
is the path to json file created in step 8Hope it helps
我必须重新启动运行时并遵循新协议。我认为我们之前使用的内容现在已被弃用...
https://colab.research.google.com/notebooks/snippets/sheets.ipynb#scrollTo=6d0xJz3VzLOo
(根据评论,复制相关片段以供将来使用)
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
(As per comment, relevant snippet copied for future posterity)