You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python中Google OAuth 400错误:redirect_uri_mismatch问题求助

Error 400: redirect_uri_mismatch when using Google OAuth flow.run_console() for YouTube Data API

我首次接触OAuth,正在开发一个需要调用YouTube Data API授权接口的Web应用,目前在本地计算机测试OAuth流程。

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

我的代码如下:

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:

访问该链接会触发Error 400: redirect_uri_mismatch错误,提示重定向URI不匹配。

由于是本地测试,我已在OAuth客户端ID中设置授权重定向URI为http://127.0.0.1:8080,同时在Python代码中也将flow.redirect_uri设为该地址。目前前端尚未搭建,这是我唯一可用的选项。

我本以为授权URI与redirect_uri匹配就能完成授权,但仍出现错误。另外,我在Google OAuth Playground中运行该流程没有任何问题。

恳请各位提供帮助,谢谢。

内容的提问来源于stack exchange,提问作者gbiz123

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 11:20:33