使用Python Gspread访问Google Drive Spreadsheets

时间:2015-07-10 13:10:20

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

我刚开始使用Gspread,并且正在尝试访问我的google驱动器中的一个google docs电子表格。我按照说明操作,转到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)

我的下一步是尝试打开Goog​​le电子表格。我创建了一个标题为" Mike"在我的Google云端硬盘的主文件夹中,但尝试通过以下方式访问它:

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访问是通过项目链接到另一个云存储,而不是我个人的谷歌驱动器,这就是为什么它不访问它。如果有更多经验的人请指出我正在做错的方向。所有帮助表示赞赏。谢谢。

3 个答案:

答案 0 :(得分:4)

您需要将使用JSON密钥创建的电子邮件添加到您要访问的电子表格中。它将像9876 ..... @ developer.gserviceaccount.com。您将其视为"客户电子邮件"在您的JSON文件和凭据页面中。

答案 1 :(得分:1)

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

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

答案 2 :(得分:1)

import {ReactInstance} from 'react-360-web'; function init(bundle, parent, options = {}) { const r360 = new ReactInstance(bundle, parent, { // Add custom options here fullScreen: true, ...options, }); // Render your app content to the default cylinder surface r360.renderToSurface( r360.createRoot('Hello360', { /* initial props */ }), r360.getDefaultSurface() ); } window.React360 = {init}; has been deprecated

相关问题