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

调用YouTube Reporting API时OAuth认证报错求助

解决方案

针对Desktop Application OAuth的OOB流错误

Google已全面禁用OAuth的OOB(Out-of-Band)流,Desktop类型凭证现在必须使用本地服务器重定向方式:

  • 在Google控制台的Desktop应用凭证页,添加http://localhost:8888(或你指定的端口)作为重定向URI;
  • 替换代码中旧的OOB流调用,改用run_local_server方法,明确指定端口与配置的URI一致:
from google_auth_oauthlib.flow import InstalledAppFlow

SCOPES = ['https://www.googleapis.com/auth/yt-analytics.readonly']

flow = InstalledAppFlow.from_client_secrets_file(
    'client_secret.json', SCOPES)
# 端口必须和控制台配置的重定向URI端口匹配
credentials = flow.run_local_server(port=8888)
  • 禁止使用run_console()方法,该方法依赖OOB流,已被废弃。

针对Web Application OAuth的redirect_uri不匹配错误

提示中出现urn:ietf:wg:oauth:2.0:oob说明代码仍在发送OOB URI,需彻底修正:

  • 确保Google控制台Web应用凭证的重定向URI与代码完全一致(比如http://localhost:8888/oauth2callback,注意路径、端口、大小写无差异);
  • 重新下载client_secret.json文件,避免本地文件的redirect_uris字段与控制台配置不同步;
  • 代码中手动指定redirect_uri,避免库默认使用OOB:
from google_auth_oauthlib.flow import Flow

SCOPES = ['https://www.googleapis.com/auth/yt-analytics.readonly']

flow = Flow.from_client_secrets_file(
    'client_secret.json',
    scopes=SCOPES,
    redirect_uri='http://localhost:8888/oauth2callback')

# 生成授权URL并引导用户访问
authorization_url, state = flow.authorization_url(
    access_type='offline',
    include_granted_scopes='true')

# 处理回调URL,获取凭证
credentials = flow.fetch_token(authorization_response='用户跳转后的完整回调URL')

通用排查步骤

  • 确认OAuth consent screen中已添加https://www.googleapis.com/auth/yt-analytics.readonly权限,外部测试需发布应用或添加测试账号;
  • 删除本地OAuth缓存文件(路径:~/.credentials/或C:\Users\<用户名>\.credentials),避免旧缓存干扰;
  • 更新依赖库到最新版本:
pip install --upgrade google-auth-oauthlib google-auth-httplib2

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 17:42:24