【发布时间】:2021-04-30 03:19:15
【问题描述】:
我正在尝试使用 Airflow 2.0 GoogleDriveHook 将本地文件上传到我的 Google Workspace 的云端硬盘。我不熟悉 OAuth 2.0。
我采取了以下步骤:
在 GCP I 中:
- 在 GCP 上创建
airflow-drive项目 - 为此项目启用了 Google Drive API
- 创建了
airflow-drive-service-account服务帐户并为其赋予(项目)“所有者”和“服务帐户令牌创建者”角色。 - 为
airflow-drive-service-account创建了一个json-key
在气流 I 中:
- 创建了 Airflow Connection
airflow-drive并指定了 json-key 的路径 - 编写了以下代码来测试
upload_file方法:
from airflow.providers.google.suite.hooks.drive import GoogleDriveHook
hook = GoogleDriveHook(
api_version='v3',
gcp_conn_id='airflow-drive',
delegate_to=None,
impersonation_chain=None
)
hook.upload_file(
local_location='some_file.txt',
remote_location='/some_file.txt'
)
这是我在运行脚本时收到的错误:
[2021-01-26 10:01:08,286] {http.py:126} WARNING - Encountered 403 Forbidden with reason "insufficientPermissions"
Traceback (most recent call last):
File "test_drive_hook.py", line 11, in <module>
hook.upload_file(
File "/home/airflow/.local/lib/python3.8/site-packages/airflow/providers/google/suite/hooks/drive.py", line 144, in upload_file
service.files() # pylint: disable=no-member
File "/home/airflow/.local/lib/python3.8/site-packages/googleapiclient/_helpers.py", line 134, in positional_wrapper
return wrapped(*args, **kwargs)
File "/home/airflow/.local/lib/python3.8/site-packages/googleapiclient/http.py", line 915, in execute
raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://www.googleapis.com/upload/drive/v3/files?fields=id&alt=json&uploadType=multipart returned "Insufficient Permission: Request had insufficient authentication scopes.". Details: "Insufficient Permission: Request had insufficient authentication scopes.">
问题:
- 我不明白我必须在哪里配置身份验证范围。 This SO question 谈论使用 SCOPES 变量定义范围。但我不确定如何为 Airflow / Google Drive API 执行此操作。
- 目前还不清楚我的用例是否必须使用
delegate_to和/或impersonation_chain参数。 This Airflow issue 涉及这个主题,但没有说明我的用例是否需要域范围委派。
【问题讨论】:
标签: oauth-2.0 google-drive-api google-oauth airflow