gpt4 book ai didi

一段时间后Python Gspread 401错误

转载 作者:行者123 更新时间:2023-12-05 06:42:49 24 4
gpt4 key购买 nike

我正在尝试编写一个程序来获取我在谷歌电子表格中记录的统计数据,并将它们写入一个文本文件,以便在流中读取和显示。该程序在大多数情况下运行良好,但偶尔会因以下原因而崩溃。这看起来像是一个 oauth 问题,但我不确定我做错了什么。

Traceback (most recent call last):
File "updatestats.py", line 67, in <module>
losses = worksheet.acell('C4').value
File "C:\Python35\lib\site-packages\gspread\models.py", line 312, in acell
return self.cell(*(self.get_int_addr(label)))
File "C:\Python35\lib\site-packages\gspread\models.py", line 328, in cell
self._cell_addr(row, col))
File "C:\Python35\lib\site-packages\gspread\client.py", line 269, in get_cells_cell_id_feed
r = self.session.get(url)
File "C:\Python35\lib\site-packages\gspread\httpsession.py", line 75, in get
return self.request('GET', url, **kwargs)
File "C:\Python35\lib\site-packages\gspread\httpsession.py", line 71, in request
response.status_code, response.content))
gspread.exceptions.HTTPError: 401: b'<HTML>\n<HEAD>\n<TITLE>Unauthorized</TITLE>\n</HEAD>\n<BODY BGCOLOR="#FFFFFF" TEXT="#000000">\n<H1>Unauthorized</H1>\n<H2>Error 401</H2>\n</BODY>\n</HTML>\n'

这是我的代码:

import requests, gspread
import os
from oauth2client.service_account import ServiceAccountCredentials

def authenticate_google_docs():
f = open(os.path.join('Culling-89b347145798.p12'), 'rb')
SIGNED_KEY = f.read()
f.close()
scope = ['https://spreadsheets.google.com/feeds', 'https://docs.google.com/feeds']
credentials = ServiceAccountCredentials('*****@gmail.com', SIGNED_KEY, scope)

data = {
'refresh_token' : '****************************',
'client_id' : '*******************************',
'client_secret' : '****************************',
'grant_type' : 'refresh_token',
}

r = requests.post('https://accounts.google.com/o/oauth2/token', data = data)
credentials.access_token = eval(r.text)['access_token']

gc = gspread.authorize(credentials)
return gc
gc = authenticate_google_docs()
sh = gc.open("The Culling")
worksheet = sh.worksheet("Stats")
while(1==1):
solo = input('Was it a solo game? ')
win = input('Were you victorious? ')
kills = input('How many kills did you get? ')
dmg = input('How much damage dvid you do? ')
if solo != "y":
if win == "y":
bAlive = input('Did you both survive? ')
gun = input('Did you find a gun? ')

if solo == "n":
if win == "y":
wins = worksheet.acell('B2').value
worksheet.update_acell('B2', int(wins)+1)
if bAlive == "y":
doubleWins = worksheet.acell('D2').value
worksheet.update_acell('D2',int(doubleWins)+1)
if win != "y":
losses = worksheet.acell('C2').value
worksheet.update_acell('C2', int(losses)+1)

if solo == "j":
if win == "y":
wins = worksheet.acell('B3').value
worksheet.update_acell('B3', int(wins)+1)
if bAlive == "y":
doubleWins = worksheet.acell('D3').value
worksheet.update_acell('D3',int(doubleWins)+1)
if win != "y":
losses = worksheet.acell('C3').value
worksheet.update_acell('C3', int(losses)+1)

if solo == "t":
if win == "y":
wins = worksheet.acell('B4').value
worksheet.update_acell('B4', int(wins)+1)
if bAlive == "y":
doubleWins = worksheet.acell('D4').value
worksheet.update_acell('D4',int(doubleWins)+1)
if win != "y":
losses = worksheet.acell('C4').value
worksheet.update_acell('C4', int(losses)+1)

if gun == "y":
if win == "y":
if solo != "y":
gWins = worksheet.acell('M9').value
worksheet.update_acell('M9', int(gWins)+1)
if solo == "y":
gWins = worksheet.acell('M10').value
worksheet.update_acell('M10', int(gWins)+1)
if win == "n":
if solo != "y":
gLosses = worksheet.acell('N9').value
worksheet.update_acell('N9', int(gLosses)+1)
if solo == "y":
gLosses = worksheet.acell('N10').value
worksheet.update_acell('N10', int(gLosses)+1)

if solo != "y":
damage = worksheet.acell('D9').value
worksheet.update_acell('D9', int(dmg)+int(damage))
klls = worksheet.acell('B9').value
worksheet.update_acell('B9', int(klls)+int(kills))
if win == "y":
streak = worksheet.acell('J9').value
bestStreak = worksheet.acell('I9').value
if bestStreak == streak:
worksheet.update_acell('I9',int(streak)+1)
worksheet.update_acell('J9',int(streak)+1)
if win != "y":
worksheet.update_acell('J9',0)
if solo == "y":
damage = worksheet.acell('D10').value
worksheet.update_acell('D10', int(dmg)+int(damage))
klls = worksheet.acell('B10').value
worksheet.update_acell('B10', int(klls)+int(kills))
if win == "y":
streak = worksheet.acell('J10').value
bestStreak = worksheet.acell('I10').value
if bestStreak == streak:
worksheet.update_acell('I10',int(streak)+1)
worksheet.update_acell('J10',int(streak)+1)
if win != "y":
worksheet.update_acell('J10',0)

最佳答案

不确定你是否解决了这个问题,类似问题有答案here .一旦出现401,重新登录即可解决。

gc.login()

关于一段时间后Python Gspread 401错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35966286/

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