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

Google API账号认证时代码未生成access token导致服务崩溃如何解决

问题排查方案
  • 确认OAuth凭证类型:在Google Cloud控制台创建的OAuth 2.0客户端ID必须为「桌面应用」类型,若创建时选择了Web应用、移动端应用等其他类型,使用InstalledAppFlow完成本地认证时会直接触发崩溃。
  • 确认文件路径正确性:检查项目根目录下确实存在名为credentials.json的凭证文件,文件内容为标准JSON格式,无下载损坏、后缀名误改问题。
  • 清理历史无效凭证:删除代码运行目录下生成的token files文件夹全部内容,避免损坏的历史缓存token干扰认证流程。
  • 解决端口占用问题:若认证弹出浏览器后跳转失败,可修改run_local_server代码指定未被占用的端口:
    将cred = flow.run_local_server()修改为cred = flow.run_local_server(port=8080)
  • 补充错误日志定位崩溃点:将Google.py中Create_Service函数的异常捕获部分修改为如下内容,打印完整异常栈确定具体报错环节:
import traceback
try:
    service = build(API_SERVICE_NAME, API_VERSION, credentials=cred)
    print(API_SERVICE_NAME, API_VERSION, 'service created successfully')
    return service
except Exception as e:
    print(f"报错信息:{e}")
    traceback.print_exc()
    print(f'Failed to create service instance for {API_SERVICE_NAME}')
    if os.path.exists(os.path.join(working_dir, token_dir, pickle_file)):
        os.remove(os.path.join(working_dir, token_dir, pickle_file))
    return None
  • 升级相关依赖包:运行以下命令升级Google认证相关依赖,解决旧版本依赖的兼容问题:
    pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 18:06:08