问题描述
我想使用 Gmail API 从我计算机上的 Python 脚本发送电子邮件。
这是我到目前为止的代码:
test.py
from google_auth_oauthlib.flow import InstalledAppFlow
flow = InstalledAppFlow.from_client_secrets_file('client_secret.json',scopes=['email'],redirect_uri='http://localhost/authorize/')
flow.run_local_server()
我对这个问题的理解是,我需要有一个 redirect uri
与我的谷歌云平台仪表板上的应用程序相匹配。唯一的问题是我只想在本地运行它,所以我放置了 uri http://localhost/authorize
。
为了确认,我确实在 Google Cloud Platform 上将 http://localhost/authorize
添加到我的 redirect uri
。
我引用了我找到的这个答案,但找不到我的解决方案: Correct redirect URI for Google API and OAuth 2.0
解决方法
我发现我需要一个重定向 URI,因为我为 Web 应用程序而不是桌面应用程序制作了 OAuth2 密钥。在这种情况下,不需要重定向 uri。