使用Google Docs API创建文档时遇AttributeError: Credentials无request属性错误
问题排查与解决
错误原因
你遇到的AttributeError("'Credentials' object has no attribute 'request'")是因为调用build()方法时参数传递错误。googleapiclient.discovery.build()的参数顺序为:服务名称、API版本,后续可选参数需要通过关键字指定。你直接将凭证对象作为位置参数传入,导致build方法错误地将其识别为其他参数,内部逻辑尝试访问凭证对象不存在的request属性,最终触发报错。
解决步骤
修正build()方法的调用方式,明确使用credentials关键字参数传递凭证:
# 修正后的服务构建代码 document_service = build('docs', 'v1', credentials=self.document_credentials)
额外验证点
- 确认你导入的
build函数正确:from googleapiclient.discovery import build - 可以打印
type(document_credentials)验证凭证类型,正常应为google.oauth2.credentials.Credentials或兼容的授权对象
内容的提问来源于stack exchange,提问作者Jack tileman
相关产品推荐
相关产品推荐

