【问题标题】:Google OAuth error 400: redirect_uri_mismatch in PythonGoogle OAuth 错误 400:Python 中的 redirect_uri_mismatch
【发布时间】:2022-11-05 00:20:58
【问题描述】:

第一次在这里使用 OAuth,我被卡住了。我正在构建一个需要对 YouTube 数据 API 进行授权调用的网络应用程序。我正在从本地计算机测试 OAuth 流程。

当我尝试在 Python 中运行我的 Google OAuth 流程时,我被卡住了接收Error 400: redirect_uri_mismatch。访问flow.run_console()生成的链接时出现错误

这是我的代码:

os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
client_secrets_file="./client_secret.json"
scopes = ["https://www.googleapis.com/auth/youtube.readonly"]
flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
client_secrets_file, scopes)
flow.redirect_uri = "http://127.0.0.1:8080" # Authorized in my client ID
credentials = flow.run_console() 

此代码返回消息:

Please visit this URL to authorize this application: ***google oauth url ***
Enter the authorization code:

访问该链接会导致以下错误:

我尝试将我的 OAuth 客户端 ID 中的授权重定向 URI 设置为 http://127.0.0.1:8080,因为我正在本地机器上进行测试。我还在 Python 中将flow.redirect_uri 设置为http://127.0.0.1:8080。使用http://127.0.0.1:8080 目前是我唯一的选择,因为前端尚未设置。

我希望代码能够授权我的请求,因为授权 URI 与 redirect_uri 匹配。但我仍然收到错误。

如果这意味着什么,我从 Google 的 OAuth Playground 运行流程没有任何问题。

任何帮助表示赞赏,谢谢。

【问题讨论】:

    标签: python oauth google-oauth youtube-data-api google-api-python-client


    【解决方案1】:

    将 redirect_uri 更改为 http://127.0.0.1http://localhost。我在使用 Google Drive API 之前遇到过类似的问题,删除端口号对我有用。

    【讨论】:

      猜你喜欢
      • 2021-10-14
      • 2021-11-05
      • 1970-01-01
      • 2021-06-28
      • 2021-11-13
      • 1970-01-01
      • 2014-02-09
      • 2015-04-03
      • 2022-10-11
      相关资源
      最近更新 更多