gpt4 book ai didi

python-3.x - Google Drive API Python 服务帐户示例

转载 作者:行者123 更新时间:2023-12-04 12:16:07 26 4
gpt4 key购买 nike

是否可以使用 Google 服务帐户而不是 OAuth 流对 Google Drive API 进行身份验证?
Google Drive 的 Python 示例使用 OAuth - Google drive Python Quick start
但是我找不到任何服务帐户示例。
但是,我使用的大多数其他 Google API(Translate、Cloud Vision)确实使用服务帐户,因此我想弃用我的 Google Drive OAuth 代码以保持一致性。

最佳答案

我所知道的最好的服务帐户 python 示例是 Google analytics 的示例。
它应该是这样的。

from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials


SCOPES = ['https://www.googleapis.com/auth/drive.readonly']
KEY_FILE_LOCATION = '<REPLACE_WITH_JSON_FILE>'
VIEW_ID = '<REPLACE_WITH_VIEW_ID>'


def initialize_drive():
"""Initializes an service object.

Returns:
An authorized service object.
"""
creds = ServiceAccountCredentials.from_json_keyfile_name(
KEY_FILE_LOCATION, SCOPES)

# Build the service object.
service = build('drive', 'v3', credentials=creds)

return service
拥有驱动器服务后,您应该能够使用其他教程中的其余代码。它只是不同的身份验证方法。
请记住创建服务帐户凭据而不是 Oauth 凭据。

关于python-3.x - Google Drive API Python 服务帐户示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64570647/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com