gpt4 book ai didi

python - 树莓派 python gspread OAuth 2 错误

转载 作者:行者123 更新时间:2023-12-01 09:24:46 31 4
gpt4 key购买 nike

我有一个脚本,应该每 2 分钟在 google 电子表格中附加一行。当我启动脚本时,它运行良好,但一段时间后我收到以下错误:

{
"error": {
"code": 401,
"message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
"status": "UNAUTHENTICATED"
}
}

Python 脚本

import gspread
from oauth2client.service_account import ServiceAccountCredentials
scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']

credentials = ServiceAccountCredentials.from_json_keyfile_name('auth.json', scope)
gc = gspread.authorize(credentials)

wks = gc.open('sheet').sheet1

while True:
#here is some other code and 2 minutes delay
wks.append_row(["test", "test2"])

Auth.json

{
"type": "x",
"project_id": "x",
"private_key_id": "x",
"private_key": "x",
"client_email": "x",
"client_id": "x",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://accounts.google.com/o/oauth2/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "x"
}

最佳答案

Google 发行的 token 是短期 token ,很快就会过期。 ServiceAccountCredentials 有一个 refresh 方法来获取新 token 。在您的情况下,由于它每 2 分钟调用一次,因此您可以每 2 分钟调用一次 gc.login()

while True:
gc.login()
<小时/>

我不确定为什么它不自行刷新(它应该)。仅供引用,oauth2client 已弃用,请考虑使用其他东西。以下是有关如何将其与 Authlib 一起使用的提示:https://blog.authlib.org/2018/authlib-for-gspread

关于python - 树莓派 python gspread OAuth 2 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50524674/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com