带OAuth2Client服务帐户密钥的gspread [英] gspread w/ OAuth2Client Service Account Key

查看:92
本文介绍了带OAuth2Client服务帐户密钥的gspread的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用gspread和服务帐户密钥(其他)json文件.用python 2.7持续更新google电子表格.我用运行最新Raspian Jessie的Raspberry Pi来运行它.我的oauth和gspread应该都是适用于我的平台的最新版本.我的脚本运行了一个小时(最大令牌寿命),然后停止处理错误消息:无效令牌:无状态令牌过期错误"我的代码如下

I am using gspread and a Service Account Key, Other, json file. to continually update a google spreadsheet with python 2.7. I have this running off a Raspberry Pi running the latest Raspian Jessie. my oauth and gspread should all be the latest versions available for my platform. My script runs for one hour(the max token life span),then stops working with the error message : "Invalid token: Statless token expired error" My code is as follows

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)

非常感谢任何帮助,谢谢.

Any Help would be greatly appreciated, Thank You.

推荐答案

授权每0.5/1小时过期一次(我认为这取决于您使用的两种可用连接方式中的哪一种).

Authorisation expires every 0.5/1 hour (I think it depends on which of the two available methods you use to connect).

我有一个24/7连接的Google工作表,每2秒更新一次. 几乎总是会导致读写错误的原因是授权错误,但是Google API也会向您抛出各种错误,这些错误通常会在几秒钟后解决.这是我更新单元格的功能之一,但是将您的详细信息用于auth_for_worksheet.每个操作(更新单个单元格,更新范围,读取一列值)都具有与函数类似的构造,该构造始终返回授权的工作表.这可能不是最优雅的解决方案,但是工作表已连接3个月,而且没有停机.

I have a google sheet connected 24/7 that updates every 2 seconds. Almost always the reason for a bad read/write is an authorisation error but also Google API can throw a variety of errors at you too that normally resolve after a few seconds. Here's one of my functions to update a cell, but using your details for auth_for_worksheet. Every operation (update single cell, update a range, read a column of values) has some similar construct as a function, which always returns an authorised worksheet. It's probably not the most elegant solution but the sheet has been connected for 3 months fine with no downtime.

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")

这篇关于带OAuth2Client服务帐户密钥的gspread的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆