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

Gmail API授权报错:InvalidClientSecretsError客户端密钥文件找不到求助

问题描述

尝试通过client_serect.py、gmail_read.py访问Gmail API,client_serect.json与这两个文件位于同一目录,但运行时始终报错:

oauth2client.clientsecrets.InvalidClientSecretsError: ('Error opening file', 'client_serect.json', 'No such file or directory', 2)

gmail_read.py代码片段:

# Creating a storage.JSON file with authentication details
SCOPES = 'https://www.googleapis.com/auth/gmail.modify' # we are using modify and not readonly, as we will be marking the messages Read
store = file.Storage('storage.json') 
#CLIENT_SERECT_FILE = './client_serect.json'
creds = store.get()
if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('D:/GitHub/SourceDKPCTuxa/server/client_serect.json', SCOPES)
    creds = tools.run_flow(flow, store)
GMAIL = discovery.build('gmail', 'v1', http=creds.authorize(Http()))

client_serect.json内容(已简化):

{"installed":{"client_id":"967939116536-0ljgbv24eo52d1oi5u5nervg2 (...)","redirect_uris":["http://localhost"]}}

授权成功后可获取Gmail账户访问权限。

排查与解决方法
  • 检查文件名拼写
    当前使用的文件名是client_serect.json,但Google API标准凭据文件名是client_secret.json(注意是secret而非serect)。如果下载的凭据文件原本拼写正确,重命名时写错就会导致找不到文件。确认本地文件实际文件名,确保代码中引用的文件名和实际文件名完全一致(部分系统区分文件名大小写)。

  • 验证文件路径有效性
    虽然使用了绝对路径D:/GitHub/SourceDKPCTuxa/server/client_serect.json,但手动打开该路径确认文件是否真的存在,避免路径输入错误(比如文件夹名拼写、分隔符错误)。如果文件和代码在同一目录,建议启用相对路径:

    CLIENT_SERECT_FILE = './client_serect.json'
    flow = client.flow_from_clientsecrets(CLIENT_SERECT_FILE, SCOPES)
    
  • 检查文件权限
    确保运行代码的用户对client_serect.json文件有读取权限,避免因权限不足导致无法打开文件。

  • 确认凭据文件格式
    你的client_serect.json是installed类型的凭据,符合桌面应用的授权要求,格式无问题,无需修改。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 03:57:21