【发布时间】:2016-07-05 01:24:54
【问题描述】:
我已经根据文档在下面实现了 python 代码,以便访问可通过公共链接访问的电子表格。 它每小时工作一次。如果我在成功后执行几秒钟,我会收到一个错误:
Error opening spreadsheet no element found: line 1, column 0
假设: 访问令牌的有效期为 1 小时。因此,appengine 将在一个小时后继续进行令牌刷新,从而重置整体。
问题: 此代码为每个请求请求一个新令牌。所以我该怎么做 ?保存令牌?当我尝试使用 token_to_blob 以保存令牌时,出现错误: 范围未定义
提前感谢您的帮助!
try :
credentials = AppAssertionCredentials(scope=('https://www.googleapis.com/auth/drive','https://spreadsheets.google.com/feeds','https://docs.google.com/feeds'))
logging.info("credentials")
http_auth = credentials.authorize(httplib2.Http())
authclient = build('oauth2','v2',http=http_auth)
auth2token = gdata.gauth.OAuth2TokenFromCredentials(credentials)
except Exception as details:
logging.error("Error Google credentials %s"%details)
return "Error"
try :
gd_client = gdata.spreadsheets.client.SpreadsheetsClient()
gd_client = auth2token.authorize(gd_client)
feed = gd_client.GetListFeed(<spreadsheetKey>,1)
except Exception as details:
logging.error("Error opening spreadsheet %s"%details)
return "Error"
【问题讨论】:
-
看看 gspread,它可以很容易地使用服务帐户访问电子表格。
-
您能先确定错误“未找到元素”的来源吗?尝试查看this 看看是否有帮助
-
谢谢 - 由于错误的身份验证参数,没有找到来自空答案的元素,即使电子表格是公开的
标签: python-2.7 google-app-engine google-spreadsheet-api google-oauth service-accounts