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

请求访问Google Spreadsheet文件API报错:文件未找到

问题解决:Google Drive API 返回 "File not found" 错误

核心原因:错误的fileId参数值

你的代码中直接将完整的Google Sheets URL传给了fileId参数,但Google Drive API的permissions.create接口要求fileId必须是文件的唯一ID,而非完整的访问链接。

浏览器中打开的URL格式为:

https://docs.google.com/spreadsheets/d/{FILE_ID}/edit#gid=0

你需要提取其中的{FILE_ID}部分(即你URL中的1u...4bcSqdgc),作为fileId参数的值传入。

修正后的代码示例

drive_service = build('drive', 'v3', credentials=g.gcreds_scoped)

permission_request = { 'role': 'writer', 'type': 'anyone', 'allowFileDiscovery': False }

# 提取文件ID替换原URL
file_id = "1u...4bcSqdgc"  # 这里替换为实际的文件ID
request = drive_service.permissions().create(
    body=permission_request, 
    fileId=file_id, 
    supportsAllDrives=True, 
    fields='id'
)

response = request.execute()

额外检查项

  • 文件权限范围:确保你的授权账号(服务账号或用户账号)能够访问该文件。如果文件在共享驱动器中,需确认账号已被添加到驱动器的成员列表,且拥有至少查看权限。
  • 凭据权限:验证g.gcreds_scoped包含了Drive API的必要权限,比如https://www.googleapis.com/auth/drive或https://www.googleapis.com/auth/drive.file。
  • 共享驱动器配置:如果文件不在共享驱动器中,supportsAllDrives=True参数可以保留,但如果是个人驱动器,也不会影响接口调用。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 04:09:58