【问题标题】:Airflow 2.0 GoogleDriveHook upload_file: insufficient authentication scopesAirflow 2.0 GoogleDriveHook upload_file:身份验证范围不足
【发布时间】: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.">


问题:

  1. 我不明白我必须在哪里配置身份验证范围。 This SO question 谈论使用 SCOPES 变量定义范围。但我不确定如何为 Airflow / Google Drive API 执行此操作。
  2. 目前还不清楚我的用例是否必须使用delegate_to 和/或impersonation_chain 参数。 This Airflow issue 涉及这个主题,但没有说明我的用例是否需要域范围委派。

【问题讨论】:

    标签: oauth-2.0 google-drive-api google-oauth airflow


    【解决方案1】:

    好的,问题已经解决了。

    解决办法如下:

    我上面的方法是正确的。我只需要在 Airflow Connection 的 Scopes 字段中添加 https://www.googleapis.com/auth/drive 范围:

    【讨论】:

      猜你喜欢
      • 2022-11-11
      • 2017-08-09
      • 2018-10-20
      • 1970-01-01
      • 2021-08-07
      • 2021-01-04
      • 2022-08-22
      • 2021-10-20
      • 2021-10-16
      相关资源
      最近更新 更多