我运行一个小型 python 机器人,它只是在线读取谷歌表,并在用户提示时继续将该信息发送到不和谐的服务器,并且它在 1-2 个月内没有使用,现在它不起作用。因此,似乎在调用 google auth 服务以刷新其 auth cred 时出现错误,我不知道该尝试什么,因此将不胜感激。我已经包含了完整的错误和它破坏的完整代码部分:
Ignoring exception in on_message
Traceback (most recent call last):
File "/home/pi/.local/lib/python3.7/site-packages/discord/client.py", line 312, in _run_event
await coro(*args, **kwargs)
File "/home/pi/Downloads/discordbot.py", line 32, in on_message
creds.refresh(Request())
File "/home/pi/.local/lib/python3.7/site-packages/google/oauth2/credentials.py", line 136, in refresh
self._client_secret))
File "/home/pi/.local/lib/python3.7/site-packages/google/oauth2/_client.py", line 237, in refresh_grant
response_data = _token_endpoint_request(request, token_uri, body)
File "/home/pi/.local/lib/python3.7/site-packages/google/oauth2/_client.py", line 111, in _token_endpoint_request
_handle_error_response(response_body)
File "/home/pi/.local/lib/python3.7/site-packages/google/oauth2/_client.py", line 61, in _handle_error_response
error_details, response_body)
google.auth.exceptions.RefreshError: ('invalid_grant: Bad Request', '{\n "error": "invalid_grant",\n "error_description": "Bad Request"\n}')
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
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly']
# The ID and range of a sample spreadsheet.
SAMPLE_SPREADSHEET_ID = '(replaced with generic text)'
SAMPLE_RANGE_NAME = 'league!A2:R'
TOKEN = '(replaced with generic text)'
client = discord.Client()
channel = client.get_channel(635962311693172776)
@client.event
async def on_message(message):
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
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)
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('sheets', 'v4', credentials=creds)
# Call the Sheets API
sheet = service.spreadsheets()
result = sheet.values().get(spreadsheetId=SAMPLE_SPREADSHEET_ID,
range=SAMPLE_RANGE_NAME).execute()
感谢阅读,我很感激