Gspread 无法与 Google Colab“验证用户”一起使用和 Oauth2Client

发布于 2025-01-18 12:28:55 字数 1756 浏览 3 评论 0原文

在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笑,眼淚并存 2025-01-25 12:28:55

Google Colab最近改变了其API。旧的API已被弃用,并提供了新的API。

这是由Google提供的新代码狙击

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')

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')
微暖i 2025-01-25 12:28:55

您应该更改以下的身份验证方法,以下本指南

  1. p>进入 Google Developers console

  2. 在标有“搜索APIS和Services”的框中,搜索对于“ Google Drive API”和“ Google Sheets”,并启用它

  3. 转到“ Apis&amp;服务&GT;凭证”,然后选择“创建凭据&gt;服务帐户密钥”。

  4. 填写表格

  5. 单击“创建”和“完成”。

  6. 按服务帐户上方的“管理服务帐户”。

  7. 按⋮最近创建的服务帐户,然后选择“管理密钥”,然后单击“添加键&gt;创建新密钥”。

  8. 选择JSON键类型,然后按“创建”。

您将获得这样的JSON:

{
    "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. 转到您的电子表格,并在上面的步骤中与client_email共享。就像您使用其他任何Google帐户一样。

  2. 将JSON文件放入驱动器中的文件夹中

  3. 将您的单元格更改为:

import gspread

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

gc = gspread.service_account(filename=json_dir)

where json_dir是步骤8中创建的JSON文件的路径,

希望它有帮助

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

罪#恶を代价 2025-01-25 12:28:55

我必须重新启动运行时并遵循新协议。我认为我们之前使用的内容现在已被弃用...

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

(根据评论,复制相关片段以供将来使用)

from google.colab import auth
auth.authenticate_user()

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

gc = gspread.authorize(creds)

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)

from google.colab import auth
auth.authenticate_user()

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

gc = gspread.authorize(creds)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文