我一直在搜索(失败)一种确定Oauth2令牌剩余时间的方法。我已经确定刷新令牌仅在令牌过期后才起作用(因此我不能任意刷新令牌)。这让我有一个问题,因为我计划在for循环中收集数据,将多行写入google工作表。
我担心的是,我的令牌将在写入Google工作表期间过期。除非我可以预测令牌何时到期,否则这给我留下了两个丑陋的选择:
跟踪令牌时间
对每一行写入使用Try / exception。
我的测试/学习代码如下。
任何参考资料都将受到赞赏。
感谢 乔恩
#!/usr/bin/python3
# -- developed with Python 3.4.2
#
# External Resources __________________________________________________________
import time
import json
import gspread
from oauth2client.service_account import ServiceAccountCredentials
import traceback
# Initialize gspread credentials
scope = ['https://spreadsheets.google.com/feeds']
credentials = ServiceAccountCredentials.from_json_keyfile_name('MyjsonFile.json',scope)
headers = gspread.httpsession.HTTPSession(headers={'Connection': 'Keep-Alive'})
client = gspread.Client(auth=credentials, http_session=headers)
client.login()
workbook = client.open("DataLogger")
wksheet = workbook.get_worksheet(1)
# Start loop ________________________________________________________________
samplecount = 1
while True:
data_time = (time.strftime("%Y-%m-%d %H:%M:%S"))
row_data = [samplecount,data_time]
if credentials.access_token_expired:
client.login()
wksheet.append_row(row_data)
print("Number of rows in out worksheet ",wksheet.row_count)
print ("samplecount ", samplecount, row_data)
print()
samplecount += 1
time.sleep(16*60)