Python / Google API 客户端 v3 / 确保线程安全

Python / Google API client v3 / ensuring thread safety

我正在尝试 'implement' 通过为每个 http 请求生成一个新的 http 对象来实现线程安全,如本 Google API python 客户端 documentation page.

为了进行身份验证,我使用了一个服务帐户,似乎有 2 种方法可以生成此 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

嗯,我迷路了。 请问,您知道问题出在哪里吗? 谢谢你的帮助。 最佳

据我所知 documentation 参数 requestBuilder 需要函数引用而不是对象(注意 requestBuilder=build_request 中缺少的括号)。确保你的函数具有与示例匹配的签名。