【发布时间】: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