【问题标题】:Accessing Google Drive Spreadsheets with Python Gspread使用 Python Gspread 访问 Google Drive 电子表格
【发布时间】:2015-07-10 13:10:19
【问题描述】:

我刚开始使用 Gspread,并试图在我的谷歌驱动器中访问我的一个谷歌文档电子表格。我按照说明前往 Google API 控制台并创建了一个 JSON 文件。当我运行此代码时,我没有收到任何错误:

import gspread
import json
from oauth2client.client import SignedJwtAssertionCredentials

json_key = json.load(open("mwsSearch-b3d5d5d9c956.json"))
scope = ["https://spreadsheets.google.com/feeds"]
credentials = SignedJwtAssertionCredentials(json_key['client_email'],  bytes(json_key['private_key'], 'utf-8'), scope=scope)
gc = gspread.authorize(credentials)

我的下一步是尝试打开一个谷歌电子表格。我在 Google Drive 的主文件夹中创建了一个名为“Mike”的电子表格,但尝试通过以下方式访问它:

gc.open_by_url("https://docs.google.com/spreadsheets/d/1HH7BKsnB2Rd5rlAr7S2H3avUtO4GkqeWrXJfYKKooNA/edit#gid=0")
gc.open("Mike")
gc.open_by_key("1HH7BKsnB2Rd5rlAr7S2H3avUtO4GkqeWrXJfYKKooNA")

这三个都返回相同的错误:

gspread.exceptions.SpreadsheetNotFound

我在想,也许 api 访问是通过项目链接到另一个云存储,而不是我个人的谷歌驱动器,这就是它没有访问它的原因。在这方面有更多经验的人可以指出我做错了什么的正确方向。感谢所有帮助。谢谢。

【问题讨论】:

    标签: python google-api google-sheets google-api-python-client gspread


    【解决方案1】:

    您需要将使用 JSON 密钥创建的电子邮件添加到您要访问的电子表格中。它将类似于 9876.....@developer.gserviceaccount.com。您会在 JSON 文件和凭证页面中找到它作为“客户电子邮件”。

    【讨论】:

    • 您的意思是您必须与该帐户共享电子表格吗?
    【解决方案2】:

    SignedJwtAssertionCredentialshas been deprecated.

    【讨论】:

      【解决方案3】:

      http://gspread.readthedocs.org/en/latest/oauth2.html

      转到 Google 表格并与您的 json_key['client_email'] 中的电子邮件共享您的电子表格。否则,您在尝试打开时会收到 SpreadsheetNotFound 异常。

      【讨论】:

        猜你喜欢
        • 2012-12-14
        • 1970-01-01
        • 1970-01-01
        • 2015-07-31
        • 1970-01-01
        • 1970-01-01
        • 2016-10-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多