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

如何修复已过期的refresh token?gspread Python应用认证故障求助

解决gspread认证invalid_grant错误的步骤

1. 重新生成认证令牌

停用一年后,旧token.json中的刷新令牌大概率已过期或被Google回收:

  • 删除本地的token.json文件
  • 重新运行Python应用,按提示完成OAuth授权流程,生成新的token.json

2. 检查OAuth客户端状态

  • 登录Google Cloud控制台,找到对应OAuth 2.0客户端ID
  • 确认客户端未被禁用,状态为「已发布」(测试状态的客户端超过期限会自动失效)
  • 核对授权重定向URI与代码配置一致(gspread默认使用http://localhost:8080/)

3. 验证系统时间同步

invalid_grant错误可能由本地系统与Google服务器时间偏差过大导致:

  • 确保服务器/本地机器开启自动时间同步,时间偏差不超过5分钟

4. 确认API权限启用状态

  • 在Google Cloud控制台API库中,检查Google Sheets API和Google Drive API是否已启用
  • 若此前启用过,可能被自动禁用,需重新启用

5. 核对OAuth授权范围

代码中需包含访问Sheets和Drive的正确权限范围,示例:

SCOPES = [
    'https://www.googleapis.com/auth/spreadsheets',
    'https://www.googleapis.com/auth/drive'
]

确保代码中的范围与控制台客户端的授权范围一致


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 10:02:37