我正试图通过在Google API python客户端文档页面中介绍的每个http请求生成一个新的http
对象来"实现"线程安全性。
对于身份验证,我使用了一个seevice帐户,似乎有两种方法可以生成这个http
对象,并提供所需的凭据。我尝试了这些,得到了以下错误:
1/
from google.oauth2 import service_account
from googleapiclient import discovery, http
# Get authorization
credentials = service_account.Credentials.from_service_account_file('/home/me/Documents/code/store3.json')
scoped_credentials = credentials.with_scopes(['https://www.googleapis.com/auth/drive'])
def build_request(creds):
new_http = creds.authorize(build_http())
return http.HttpRequest(new_http)
drive = discovery.build('drive', 'v3', credentials=scoped_credentials, requestBuilder=build_request(scoped_credentials))
则错误为:AttributeError: 'Credentials' object has no attribute 'authorize'
2/
from google.oauth2 import service_account
from googleapiclient import discovery, http
# Get authorization
credentials = service_account.Credentials.from_service_account_file('/home/me/Documents/code/store3.json')
scoped_credentials = credentials.with_scopes(['https://www.googleapis.com/auth/drive'])
import google_auth_httplib2
def build_request(creds):
return google_auth_httplib2.AuthorizedHttp(creds, http=http.build_http())
drive = discovery.build('drive', 'v3', credentials=scoped_credentials, requestBuilder=build_request(scoped_credentials))
results = drive.files().list(pageSize=10, fields="nextPageToken, files(id, name)").execute()
则错误为:TypeError: 'AuthorizedHttp' object is not callable
嗯,我迷路了。拜托,你知道麻烦可能来自哪里吗?谢谢你的帮助。最佳
据我从文档中所见,参数requestBuilder
需要的是函数引用,而不是对象(注意requestBuilder=build_request
中缺少的括号。确保函数具有与示例匹配的签名。