我使用的是gspread和一个服务帐户密钥,其他,json文件。使用python2.7持续更新google电子表格。我有一个树莓派运行最新的Raspian杰西。我的oauth和gspread都应该是适用于我的平台的最新版本。我的脚本运行一个小时(最大令牌寿命),然后停止工作,并显示以下错误消息:"Invalid token: Statless token expired error“我的代码如下
import gspread
from oauth2client.service_account import ServiceAccountCredentials
import httplib2
from httplib2 import Http
scope = ['https://spreadsheets.google.com/feeds']
credentials = ServiceAccountCredentials.from_json_keyfile_name(filename.json,scope)
gc = gspread.authorize(credentials)
wks = gc.open('spreadsheet name')
p1 = wks.worksheet('Printer One')
def functon()
...
p1.append_row(printing)任何帮助都将不胜感激,谢谢。
发布于 2016-08-12 01:47:08
授权每0.5小时到期一次(我认为这取决于您使用两种可用方法中的哪一种进行连接)。
我有一个24/7连接的谷歌工作表,每2秒更新一次。读/写不好的原因几乎总是授权错误,但Google API也会抛出各种各样的错误,这些错误通常在几秒钟后就会解决。这是我的一个更新单元格的函数,但使用auth_for_worksheet的详细信息。每个操作(更新单个单元格、更新一个范围、读取一列值)都有一些类似的构造作为函数,它总是返回一个授权的工作表。这可能不是最优雅的解决方案,但工作表已经连接了3个月,没有停机。
def auth_for_worksheet():
scope = ['https://spreadsheets.google.com/feeds']
credentials = ServiceAccountCredentials.from_json_keyfile_name(filename.json,scope)
gc = gspread.authorize(credentials)
wks = gc.open('spreadsheet name')
p1 = wks.worksheet('Printer One')
return p1
def update_single_cell(worksheet, counter, message):
""" No data to return, update a single cell in column B to reflect this """
single_cell_updated = False
while not single_cell_updated:
try:
cell_location = "B" + str(counter)
worksheet.update_acell(cell_location, message)
single_cell_updated = True
except gspread.exceptions.HTTPError:
logger.critical("Could not update single cell")
time.sleep(10)
worksheet = auth_for_worksheet()
logger.info("Updated single cell")
return worksheet
if __name__ == '__main__':
# your code here, but now to update a single cell
wksheet = update_single_cell(wksheet, x, "NOT FOUND")https://stackoverflow.com/questions/38880188
复制相似问题