如何使用python gspread

时间:2016-04-18 15:22:45

标签: python oauth-2.0 gspread

经过2天阅读gspread docs,gspread博客帖子,并且追随recent docs for using gspread以后,我仍然无法打开一个Google电子表格。我设置了GDrive API服务帐户。我的OAuth2凭据似乎正在运行,但我仍然会收到“SpreadsheetNotFound”错误。

我的代码看起来像这样,在尝试打开Goog​​le电子表格之前运行时没有出现任何错误消息:

import gspread
from oauth2client.service_account import ServiceAccountCredentials

scope = ['https://spreadsheets.google.com/feeds']

jsonfile    = '/path/Python-data-GSpreadsheets-dbbc-99.json'
credentials = ServiceAccountCredentials.from_json_keyfile_name(jsonfile,scope)

# get authorized to access data
gc = gspread.authorize(credentials)
# gc returns with no error messages, so far OK
wks = gc.open('SSTEst123').sheet1  #fails here
#throws SpreadsheetNotFound error

是的,我已经添加了我的gmail电子邮件地址作为授权编辑(尝试使用和不使用),因为服务帐户应该能够访问我的所有数据,并且我正在重新整理我的电子表格。

我还能尝试发现它失败的原因还有什么? 有关如何调试无法打开任何电子表格的原因的任何想法?

好的,这就是我解决它的方法。 不得不与Google Drive API服务帐户“电子邮件地址”分享我的电子表格(例如my.test.data@python-xyz-gspreadsheets.iam.gserviceaccount.com) - (我认为这是我'真正的'gmail ID /电子邮件,但事实并非如此)

用于调试Feed ... 在gspread client.py源中添加了一些print()行,以查看我是否在Feed中获取了任何内容,并检查了Feed中的值。注意到我得到的值是旧值(来自以前的脚本?)

关闭并重新启动我的iPython笔记本,它现在正在运行。

1 个答案:

答案 0 :(得分:0)

好的,问题解决了。这就是我做的事情:

在client.py源中添加了一些print()行,以查看我是否在Feed中获取了任何内容,并检查了Feed中的值。注意到我得到的值是旧值(来自以前的脚本?)

关闭并重新启动我的iPython笔记本,它现在正在工作。