【问题标题】:Python / Google API client v3 / ensuring thread safetyPython / Google API 客户端 v3 / 确保线程安全
【发布时间】:2020-07-10 10:55:14
【问题描述】:

我正在尝试通过为每个 http 请求生成一个新的 http 对象来“实现”线程安全,正如此 Google API python 客户端 documentation page 中所介绍的那样。

对于身份验证,我使用的是服务帐户,并且似乎有两种方法可以生成此 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

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

【问题讨论】:

    标签: python google-api google-drive-api google-api-python-client


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2012-08-14
      • 2019-03-20
      • 1970-01-01
      • 2011-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-05
      • 2017-11-06
      相关资源
      最近更新 更多