2

我正在尝试编写一个程序来获取我在谷歌电子表格中记录的统计数据,并将它们写入一个文本文件,以便在流中读取和显示。该程序在大多数情况下运行良好,但每隔一段时间它就会崩溃并出现以下情况。它看起来像一个 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)
4

1 回答 1

0

不确定你是否解决了这个问题,这里有类似问题的答案。一旦你得到401,然后再次登录以解决它。

gc.login()
于 2019-07-26T16:29:52.870 回答