Airflow 2.0 GoogleDriveHook upload_file:身份验证范围不足

Airflow 2.0 GoogleDriveHook upload_file: insufficient authentication scopes

我正在尝试使用 Airflow 2.0 GoogleDriveHook 将本地文件上传到我的 Google Workspace 的驱动器。我对 OAuth 2.0 不熟悉。

我采取了以下步骤:

在 GCP I 中:

在气流 I 中:

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. 执行此操作
  3. 我不清楚我的用例是否必须使用 delegate_to and/or impersonation_chain 参数。 This Airflow issue 涉及到这个主题,但没有阐明我的用例是否需要全域委派。

好的,问题已经解决

解决方法如下:

我上面的做法是正确的。我只需要在气流连接的范围字段中添加 https://www.googleapis.com/auth/drive 范围: