首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用OAuth2Client服务帐户密钥扩展

使用OAuth2Client服务帐户密钥扩展
EN

Stack Overflow用户
提问于 2016-08-11 01:43:49
回答 1查看 443关注 0票数 0

我使用的是gspread和一个服务帐户密钥,其他,json文件。使用python2.7持续更新google电子表格。我有一个树莓派运行最新的Raspian杰西。我的oauth和gspread都应该是适用于我的平台的最新版本。我的脚本运行一个小时(最大令牌寿命),然后停止工作,并显示以下错误消息:"Invalid token: Statless token expired error“我的代码如下

代码语言:javascript
复制
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)

任何帮助都将不胜感激,谢谢。

EN

回答 1

Stack Overflow用户

发布于 2016-08-12 01:47:08

授权每0.5小时到期一次(我认为这取决于您使用两种可用方法中的哪一种进行连接)。

我有一个24/7连接的谷歌工作表,每2秒更新一次。读/写不好的原因几乎总是授权错误,但Google API也会抛出各种各样的错误,这些错误通常在几秒钟后就会解决。这是我的一个更新单元格的函数,但使用auth_for_worksheet的详细信息。每个操作(更新单个单元格、更新一个范围、读取一列值)都有一些类似的构造作为函数,它总是返回一个授权的工作表。这可能不是最优雅的解决方案,但工作表已经连接了3个月,没有停机。

代码语言:javascript
复制
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")
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38880188

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档