【问题标题】:Python discord bot that calls the google sheets API issue调用谷歌表格API问题的Python不和谐机器人
【发布时间】:2020-10-05 14:22:47
【问题描述】:

我现在在我的不和谐机器人上随机遇到一个奇怪的错误。它使用 python 并且只调用 Google Sheets API 并打印它在一系列单元格中找到的信息。我目前在另一个房间的树莓派上运行它,它在那里工作正常,但我无法在任何地方创建机器人的另一个实例,所以我害怕重新启动树莓派(以防它无法重新登录) .我曾经能够运行多个机器人实例,所以这让我有点困惑。

这是授权代码:

import pickle
import os.path    
from googleapiclient.discovery import build    
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request   
from discord import Game

SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly']
SAMPLE_SPREADSHEET_ID = 'my spreadsheet'    
SAMPLE_RANGE_NAME = 'league!A2:R'     
TOKEN = 'Token generated a while back'

client = discord.Client()
channel = client.get_channel('my channel')
@client.event
async def on_message(message):
    
    if os.path.exists('token.pickle'):
        with open('token.pickle', 'rb') as token:
            creds = pickle.load(token)
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                'credentials.json', SCOPES)
            creds = flow.run_local_server(port=0)
        with open('token.pickle', 'wb') as token:
            pickle.dump(creds, token)

The error

我花了一些时间研究可能是什么问题,它说 Google 表格身份验证令牌可能有问题,但这些建议的修复似乎都不起作用。我将非常感谢任何帮助,因为我对 python 很陌生。

【问题讨论】:

  • 您是否尝试过在发出请求时使用 API 资源管理器来验证您是否拥有正确的参数?还有,代码在哪一行失败了?
  • 我确实查看了 API 文档并完全重置了我的工作区(卸载了 python 并重新安装了所有必需的文件),它现在正在工作。我认为其中一个文件已损坏或其他什么。谢谢你的回复。

标签: python api google-sheets


【解决方案1】:

pip 和 googleAPI 文件一定以某种方式损坏了,因为完全破坏和重置我的工作区似乎已经修复了它。

【讨论】:

    猜你喜欢
    • 2020-06-08
    • 2022-11-10
    • 2020-07-22
    • 1970-01-01
    • 2021-09-26
    • 2021-01-25
    • 1970-01-01
    • 2019-02-26
    相关资源
    最近更新 更多