Jus*_*tin 6 google-api python-3.x google-drive-api google-api-python-client service-accounts
是否可以使用 Google 服务帐户(而不是 OAuth 流程)对 Google Drive API 进行身份验证?
Google Drive 使用 OAuth 的 Python 示例 - Google Drive Python 快速入门
但是我找不到任何服务帐户示例。
然而,我使用的大多数其他 Google API(Translate、Cloud Vision)确实使用服务帐户,因此为了保持一致性,我想弃用我的 Google Drive OAuth 代码。
我所知道的最好的服务帐户 python 示例是Google 分析的示例
应该是这样的。
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
Run Code Online (Sandbox Code Playgroud)
获得驱动服务后,您应该能够使用其他教程中的其余代码。只是身份验证方法不同。
只需记住创建服务帐户凭据而不是 Oauth 凭据。
| 归档时间: |
|
| 查看次数: |
4787 次 |
| 最近记录: |