我正在尝试使用 Python Google API,但在使用 https://developers.google.com/people/quickstart/python#configure_the_sample 中的 Google 快速入门示例时遇到问题,按原样。当我第一次运行这个示例时,它会打开 Web 浏览器,允许我进行身份验证,并且它工作得很好。但是,如果我再次运行脚本,它不会进行身份验证,而是尝试使用保存在 token.json
中的 token ,并且失败并显示:
Traceback (most recent call last):
File "/home/thomas/test.py", line 58, in <module>
main()
File "/home/thomas/test.py", line 24, in main
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
File "/home/thomas/.local/lib/python3.10/site-packages/google/oauth2/credentials.py", line 440, in from_authorized_user_file
return cls.from_authorized_user_info(data, scopes)
File "/home/thomas/.local/lib/python3.10/site-packages/google/oauth2/credentials.py", line 390, in from_authorized_user_info
raise ValueError(
ValueError: Authorized user info was not in the expected format, missing fields refresh_token.
我错过了什么?
最佳答案
我发现了问题:我在 Google Cloud 控制台中的 OAuth key 被配置为“网络应用程序”OAuth key ,而它本应是“桌面应用程序” key 。解决了这个问题后,一切都运行良好。
关于python - Google身份验证问题: Authorized user info was not in the expected format,缺少字段refresh_token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74958898/