一周前我设置了一个使用 google drive api 的 discord bot。机器人在网络托管服务器上工作了一个星期,今天我检查了一下,每次启动机器人时它都会给我这个错误。
client_secret.json-drive-v3-(['https://www.googleapis.com/auth/drive'],)
['https://www.googleapis.com/auth/drive']
Traceback (most recent call last):
File "C:\Users\Unknown1\Documents\discordbot\bot.py", line 5, in <module>
from googledrive import find_recent
File "C:\Users\Unknown1\Documents\discordbot\googledrive.py", line 10, in <module>
service = Create_Service(CLIENT_SECRET_FILE, API_NAME, API_VERSION, SCOPES)
File "C:\Users\Unknown1\Documents\discordbot\Google.py", line 28, in Create_Service
cred.refresh(Request())
File "C:\Users\Unknown1\AppData\Local\Programs\Python\Python39\lib\site-packages\google\oauth2\credentials.py", line 200, in refresh
access_token, refresh_token, expiry, grant_response = _client.refresh_grant(
File "C:\Users\Unknown1\AppData\Local\Programs\Python\Python39\lib\site-packages\google\oauth2\_client.py", line 248, in refresh_grant
response_data = _token_endpoint_request(request, token_uri, body)
File "C:\Users\Unknown1\AppData\Local\Programs\Python\Python39\lib\site-packages\google\oauth2\_client.py", line 124, in _token_endpoint_request
_handle_error_response(response_body)
File "C:\Users\Unknown1\AppData\Local\Programs\Python\Python39\lib\site-packages\google\oauth2\_client.py", line 60, in _handle_error_response
raise exceptions.RefreshError(error_details, response_body)
google.auth.exceptions.RefreshError: ('invalid_grant: Token has been expired or revoked.', '{\n "error": "invalid_grant",\n "error_description": "Token has been expired or revoked."\n}')
PS C:\Users\Unknown1\Documents\discordbot>
我检查了 client_secret_file.json 并从 google cloud api 凭据重新下载了它,它仍然是一样的东西。我不知道从哪里开始解决这个问题。
import pandas as pd
import datetime
CLIENT_SECRET_FILE = 'client_secret.json'
API_NAME = 'drive'
API_VERSION = 'v3'
SCOPES = ['https://www.googleapis.com/auth/drive']
service = Create_Service(CLIENT_SECRET_FILE, API_NAME, API_VERSION, SCOPES)
#in the google drive web link, there will be a folder id.
folder_id = '1ANm4frKaL_JeMEewRMDBLWiiY3Srh-B-'
query = f"parents = '{folder_id}'"
response = service.files().list(q=query).execute()
files = response.get('files')
nextPageToken = response.get('nextPageToken')
while nextPageToken:
response = service.files().list(q=query,pageToken=nextPageToken).execute()
files.extend(response.get('files'))
nextpageToken = response.get('nextPageToken')
df = pd.DataFrame(files)
我根据以下 YouTube 视频建立了一个新项目 OAuth 2.0 客户端 ID 凭据:https://www.youtube.com/watch?v=FAM_4J7ywcE&ab_channel=nouthemes
并删除了旧文件,希望用新文件替换客户端机密文件。现在我收到一个新错误:
client_secret.json-drive-v3-(['https://www.googleapis.com/auth/drive'],)
['https://www.googleapis.com/auth/drive']
Traceback (most recent call last):
File "C:\Users\Unknown1\Documents\discordbot\bot.py", line 5, in <module>
from googledrive import find_recent
File "C:\Users\Unknown1\Documents\discordbot\googledrive.py", line 10, in <module>
service = Create_Service(CLIENT_SECRET_FILE, API_NAME, API_VERSION, SCOPES)
File "C:\Users\Unknown1\Documents\discordbot\Google.py", line 28, in Create_Service
cred.refresh(Request())
File "C:\Users\Unknown1\AppData\Local\Programs\Python\Python39\lib\site-packages\google\oauth2\credentials.py", line 200, in refresh
access_token, refresh_token, expiry, grant_response = _client.refresh_grant(
File "C:\Users\Unknown1\AppData\Local\Programs\Python\Python39\lib\site-packages\google\oauth2\_client.py", line 248, in refresh_grant
response_data = _token_endpoint_request(request, token_uri, body)
File "C:\Users\Unknown1\AppData\Local\Programs\Python\Python39\lib\site-packages\google\oauth2\_client.py", line 124, in _token_endpoint_request
_handle_error_response(response_body)
File "C:\Users\Unknown1\AppData\Local\Programs\Python\Python39\lib\site-packages\google\oauth2\_client.py", line 60, in _handle_error_response
raise exceptions.RefreshError(error_details, response_body)
google.auth.exceptions.RefreshError: ('deleted_client: The OAuth client was deleted.', '{\n "error": "deleted_client",\n "error_description": "The OAuth client was deleted."\n}')
我被卡住了。