gpt4 book ai didi

python - 错误谷歌 API : "Credentials in post body and basic Authorization header do not match"

转载 作者:行者123 更新时间:2023-12-05 08:05:05 30 4
gpt4 key购买 nike

我已经实现了一个连接到谷歌驱动器并下载我的文件的 python 脚本。对于授权,我遵循了谷歌网站上的快速入门指南和代码示例。

问题是这样的:当我第一次授权时,生成了一个 token.pickle,但过了一段时间(不多,可能不到 1 小时)我收到了这个错误:

google.auth.exceptions.RefreshError: ('invalid_request: Credentials in post body and basic Authorization header do not match', '{\n "error": "invalid_request",\n "error_description": "Credentials in post body and basic Authorization header do not match"\n}')

如果我删除 token.pickle 并从浏览器再次进行授权,一切都会再次正常,但显然我需要一个有效期更长的 token 。

授权代码是这样的:

SCOPES = ['https://www.googleapis.com/auth/drive']

def main():
"""Shows basic usage of the Drive v3 API.
Prints the names and ids of the first 10 files the user has access to.
"""

creds = None
# 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('drive', 'v3', credentials=creds)

谢谢

最佳答案

如果您之前已经授权过该应用程序,则可能会发生这种情况。 oauth2 服务器一次只会生成一个刷新 token ,如果您通过流程请求另一个访问 token ,它将像您只请求访问 token 一样运行。

尝试通过 Manage third-party apps & services with access to your account直接通过谷歌撤销访问权限,然后再次尝试授权该应用程序。然后,您应该为用户提供一个刷新 token 。这将允许您的脚本在需要时请求新的访问 token 。

如果这不起作用,您可以尝试使用 prompt='consent' 构建流对象,看看是否能解决问题。

关于python - 错误谷歌 API : "Credentials in post body and basic Authorization header do not match",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66076036/

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