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

如何修复Google Drive API获取文档时的TypeError参数错误?

解决方法

错误原因

你调用的是Drive API的评论接口(Comments),但该接口的get方法接收的参数是fileId而非documentId;更关键的是,若要获取文档的基础信息(而非评论内容),应该使用**文件接口(Files)**的get方法。

修正后的代码

from google.oauth2.service_account import Credentials
from googleapiclient.discovery import build
import json

credentials_doc = 'service_account.json'

# 加载服务账号凭证
service_account_info = json.load(open(credentials_doc))
credentials = Credentials.from_service_account_info(service_account_info)
# 构建Drive v3服务
service = build('drive', 'v3', credentials=credentials)

document_id = 'my_doc_drive_id'

# 获取文档基础信息,fields指定返回字段,'*'表示返回所有可用字段
document = service.files().get(fileId=document_id, fields='*').execute()
print(document)

额外注意事项

  • 权限配置:必须将目标Google文档共享给服务账号的邮箱(在service_account.json的client_email字段中),否则会返回权限不足的错误。
  • 字段过滤:如果不需要所有文档信息,可以指定具体字段减少返回数据量,比如fields='id,name,mimeType,createdTime'。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 11:09:57