使用Python获取Google OAuth凭证失败:未生成credentials.json及密钥错误
解决Google OAuth凭证生成时的KeyError: 'client_secret'问题
你的问题根源很明确:当前的client_secret_NNNN.json文件缺少client_secret字段,而google-oauthlib-tool在OAuth2授权流程中必须用到这个参数,所以抛出了KeyError。
具体解决步骤:
- 打开Google Cloud Console,进入你的项目
personal-stuff-54a54 - 导航到APIs & Services > Credentials页面
- 在"OAuth 2.0 Client IDs"列表中,找到你之前创建的「Installed application」类型的客户端ID
- 点击该条目右侧的「Download JSON」按钮,重新下载完整的客户端密钥文件
- 用新下载的文件替换你当前
c:\temp\client_secret_NNNN.json文件,确认新文件里包含client_secret键值对(类似"client_secret": "xxxxxx...") - 重新运行你原来的命令:
google-oauthlib-tool --client-secrets c:\temp\client_secret_NNNN.json --credentials c:\temp\credentials.json --scope https://www.googleapis.com/auth/assistant-sdk-prototype --save
补充说明:
有时候如果创建OAuth客户端ID时选错了类型(比如选了Web应用而不是Installed application),下载的JSON文件也会缺少client_secret字段。确保你创建的是Installed application类型,这样生成的密钥文件才会包含完整的OAuth2所需参数。
内容的提问来源于stack exchange,提问作者Konstantin Konopko
相关产品推荐
相关产品推荐

