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

运行quickstart.ipynb遇Error 400: invalid_request问题求助

解决Google OAuth 400 invalid_request(OOB流程被封禁)问题

错误原因

Google出于安全考量,已封禁Out-of-Band (OOB)授权流程(对应redirect_uri=urn:ietf:wg:oauth:2.0:oob),原代码中flow.run_console()默认使用该流程,因此触发400错误。

解决方案

将原代码中的run_console()替换为run_local_server(),该方法会启动本地HTTP服务器接收授权回调,符合Google当前的安全规范。

修改后的完整代码

from apiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow

CLIENT_CONFIG = {
    'installed': {
        'client_id':'----------------------------------------------------------',
        'client_secret': '------------------------------------------------------',
        'auth_uri':'https://accounts.google.com/o/oauth2/auth',
        'token_uri':'https://oauth2.googleapis.com/token',
        # 可选:添加本地回调地址,增强配置规范性
        'redirect_uris': ['http://localhost']
    }
}
SCOPES = ['https://www.googleapis.com/auth/androidmanagement']

flow = InstalledAppFlow.from_client_config(CLIENT_CONFIG, SCOPES)
# 替换OOB流程为本地服务器回调流程,port=0自动分配可用端口
credentials = flow.run_local_server(port=0)

androidmanagement = build('androidmanagement', 'v1', credentials=credentials)

print('\nAuthentication succeeded.')

运行说明

执行修改后的代码后,系统会自动打开浏览器跳转至Google授权页面,完成登录和授权操作后,浏览器将自动回调本地服务器,无需手动复制授权码,认证完成后会输出Authentication succeeded.。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 07:26:07