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

调用LocalWebserverAuth()时GoogleAuth报invalid grant: Bad Request错误如何解决?

诊断并解决PyDrive LocalWebserverAuth的invalid_grant错误

问题场景

运行以下PyDrive认证脚本时,触发invalid_grant: Bad Request错误,且未弹出浏览器认证窗口:

from pydrive.auth import GoogleAuth

gauth = GoogleAuth()
gauth.LoadClientConfigFile("client_secrets.json")
print(f'==== called LoadClientConfigFile', flush=True)
gauth.LocalWebserverAuth()  # 触发浏览器认证

报错信息

==== called LoadClientConfigFile
Traceback (most recent call last):
  File "C:\Users\r\.virtualenvs\pygmu-oJfXpe_C\Lib\site-packages\pydrive\auth.py", line 475, in Refresh
    self.credentials.refresh(self.http)
  File "C:\Users\r\.virtualenvs\pygmu-oJfXpe_C\Lib\site-packages\oauth2client\client.py", line 545, in refresh
    self._refresh(http)
  File "C:\Users\r\.virtualenvs\pygmu-oJfXpe_C\Lib\site-packages\oauth2client\client.py", line 761, in _refresh
    self._do_refresh_request(http)
  File "C:\Users\r\.virtualenvs\pygmu-oJfXpe_C\Lib\site-packages\oauth2client\client.py", line 819, in _do_refresh_request
    raise HttpAccessTokenRefreshError(error_msg, status=resp.status)
oauth2client.client.HttpAccessTokenRefreshError: invalid_grant: Bad Request

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\r\Projects\pygmu\t1.py", line 6, in <module>
    gauth.LocalWebserverAuth()  # 触发浏览器认证
    ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\r\.virtualenvs\pygmu-oJfXpe_C\Lib\site-packages\pydrive\auth.py", line 120, in _decorated
    self.Refresh()
  File "C:\Users\r\.virtualenvs\pygmu-oJfXpe_C\Lib\site-packages\pydrive\auth.py", line 477, in Refresh
    raise RefreshError('Access token refresh failed: %s' % error)
pydrive.auth.RefreshError: Access token refresh failed: invalid_grant: Bad Request

环境信息

  • Python 3.12
  • Windows 11 Pro 版本23H2
  • 可用浏览器:Firefox、Chrome、Edge

更新说明

已注意到PyDrive代码可能过时,正在确认Google Drive API当前的官方用法。

诊断与解决方法

  • 验证client_secrets.json有效性

    • 确认文件是从Google Cloud控制台下载的桌面应用类型凭证,而非Web应用或其他类型
    • 检查文件内的client_id、client_secret字段完整无格式错误
  • 清理旧认证缓存

    • 删除本地保存的PyDrive认证缓存文件(通常是credentials.json或项目目录下的.pydrive文件夹),重新运行脚本强制发起新的认证流程
  • 检查Python版本兼容性

    • PyDrive及依赖的oauth2client库对Python 3.12的支持不完善,尝试降级到Python 3.10或3.11版本测试
  • 手动指定浏览器

    • 若脚本无法自动唤起默认浏览器,可手动指定浏览器路径:
      import webbrowser
      # 以Chrome为例,替换为你实际的浏览器路径
      webbrowser.register('chrome', None, webbrowser.BackgroundBrowser("C://Program Files//Google//Chrome//Application//chrome.exe"))
      gauth.LocalWebserverAuth(browser_name='chrome')
      
  • 替换为官方推荐的客户端库

    • PyDrive已长期未维护,官方推荐使用google-api-python-client系列库,基础认证代码示例:
      from google_auth_oauthlib.flow import InstalledAppFlow
      from google.auth.transport.requests import Request
      import pickle
      import os
      
      # 根据需求调整权限范围
      SCOPES = ['https://www.googleapis.com/auth/drive']
      creds = None
      
      # 读取缓存的认证凭证
      if os.path.exists('token.pickle'):
          with open('token.pickle', 'rb') as token:
              creds = pickle.load(token)
      
      # 刷新或重新获取凭证
      if not creds or not creds.valid:
          if creds and creds.expired and creds.refresh_token:
              creds.refresh(Request())
          else:
              flow = InstalledAppFlow.from_client_secrets_file(
                  'client_secrets.json', SCOPES)
              creds = flow.run_local_server(port=0)
          # 保存凭证供后续使用
          with open('token.pickle', 'wb') as token:
              pickle.dump(creds, token)
      
  • 排查网络与代理问题

    • 确认网络可正常访问Google服务,无代理或防火墙拦截认证请求
  • 检查Google Cloud控制台配置

    • 确认项目已启用Google Drive API
    • 若OAuth同意屏幕处于测试状态,需将当前使用的Google账号添加到测试用户列表中

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 14:01:19