【问题标题】:Not able to run google sheet api from composer无法从作曲家运行 google sheet api
【发布时间】:2020-01-22 13:23:17
【问题描述】:

我正在从计算引擎运行 google sheet extract python api,它工作正常 我从作曲家那里运行同样的方法,但它不起作用

在 composer 中,我使用相同的用户登录 vm 并运行 ssh 命令,并且我在 google sheet 和 vm 中使用相同的服务帐户

以下是我收到的错误消息

 File "/home/utetwork_multiplier.py", line 15, in sheet_reade
    result = service.spreadsheets().values().get(spreadsheetId=spreadsheet_id, range=range_name).execute(
  File "/hsite-packages/googleapiclient/_helpers.py", line 130, in positional_wrappe
    return wrapped(*args, **kwargs
  File "/on3.5/site-packages/googleapiclient/http.py", line 849, in execut
    raise HttpError(resp, content, uri=self.uri
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://sheets.googleapis.com/v4/spreadsheets/14r0cQ1RCXhLyd7i0VCrmddXOFiugFnfioRb6cYI_BWQ/values/Master%21A%3AJ?alt=json returned "Request had insufficient authentication scopes."

Traceback (most recent call last)
  File "/usr/local/lib/airflow/airflow/contrib/operators/ssh_operator.py", line 164, in execut
    .format(self.command, error_msg)
airflow.exceptions.AirflowException: error running cmd: set -e;cd src/digital_platform && ../../venvs/bdp/bin/python -m.marketing.adnetwork_multiplier, error: Traceback (most recent call last)
  File "/usr/lib/python3.5/runpy.py", line 184, in _run_module_as_mai
    "__main__", mod_spec
  File "/usr/lib/python3.5/runpy.py", line 85, in _run_cod
    exec(code, run_globals
  File "/home/tt/src/digital_platform//marketing/adnetwork_multiplier.py", line 74, in <module
    main(
  File "/home/tt/src/digital_platform//marketing/adnetwork_multiplier.py", line 28, in mai
    data = sheet_reader(range_name
  File "/home/tt/src/digital_platform//marketing/adnetwork_multiplier.py", line 15, in sheet_reade
    result = service.spreadsheets().values().get(spreadsheetId=spreadsheet_id, range=range_name).execute(
  File "/home//venvs/bdp/lib/python3.5/site-packages/googleapiclient/_helpers.py", line 130, in positional_wrappe
    return wrapped(*args, **kwargs
  File "/home//venvs/bdp/lib/python3.5/site-packages/googleapiclient/http.py", line 849, in execut
    raise HttpError(resp, content, uri=self.uri
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://sheets.googleapis.com/v4/spreadsheets/14r0cQ1RCXhLydcYI_BWQ/values/Master%21A%3AJ?alt=json returned "Request had insufficient authentication scopes."


During handling of the above exception, another exception occurred

范围

  scope = [
        'https://www.googleapis.com/auth/bigquery',
        'https://www.googleapis.com/auth/spreadsheets.readonly',
        'https://www.googleapis.com/auth/spreadsheets'
    ]

    home = os.path.expanduser('~')
    csf = os.path.join(home, '.client_secret.json')
    token_filename = os.path.join(home, '.google_auth.dat')

    flow = flow_from_clientsecrets(csf, scope=scope, message="%s is missing" % csf)
    storage = Storage(token_filename)

    credentials = storage.get()
    if credentials is None or credentials.invalid:
        flags = tools.argparser.parse_args(args=[])
        flags.noauth_local_webserver = True
        credentials = tools.run_flow(flow, storage, flags=flags)

【问题讨论】:

  • 你能说明你是如何设置范围来执行 API 调用的吗?
  • 在@Jescanellas 以上更新
  • 作用域足以做一个 getValues 请求,但它们不是多余的吗? auth/spreadsheets 已经授予读取权限。也许您更愿意将其仅设置为auth/spreadhseets.readonly。另外,请尝试删除令牌文件并再次授予权限。
  • 实际上它在本地运行良好,但是当我从作曲家运行相同的命令时它失败了,这对我来说很奇怪
  • 请编辑您的问题并包含足够的代码以供我们测试您发布的任何内容都不会针对 Google 表格提出任何请求。

标签: python google-api google-oauth google-sheets-api google-api-python-client


【解决方案1】:

从您似乎正在运行的错误消息Method: spreadsheets.values.get

该方法需要以下范围之一才能访问

错误消息状态

403 请求 https://sheets.googleapis.com/v4/spreadsheets/14r0cQ1RCXhLydcYI_BWQ/values/Master%21A%3AJ?alt=json 时返回“请求的身份验证范围不足。

这意味着您已通过身份验证的用户未通过上述所需范围之一进行身份验证。我建议您注销用户删除该用户的凭据并再次运行您的代码,以确保当它弹出同意屏幕时它要求提供所需范围之一。我怀疑您正在使用来自用户的过时授权来运行您的脚本。

【讨论】:

  • 问题是我可以直接在机器上运行它 - 但是当从 composer 运行命令时使用同一用户不起作用
  • t_coupon_usage_for_affiliates = SSHOperator( ssh_conn_id='bdp_vm', task_id='coupon_usage_for_affiliates', command=coupon_usage_for_affiliates, on_failure_callback=slack_failed_task, dag=dag) 我在作曲家中像上面那样做
  • 当我使用 Jenkins 运行脚本时它工作正常
  • 听起来你没有先授权你的代码。即使您在机器上运行它,它仍然需要进行身份验证,并且如果您使用 oauth2 需要用户干预或保存您的代码可以加载的刷新令牌
  • 我用同一个用户登录
猜你喜欢
  • 1970-01-01
  • 2015-10-23
  • 1970-01-01
  • 1970-01-01
  • 2018-04-22
  • 2021-12-19
  • 2018-04-26
  • 1970-01-01
  • 2017-08-13
相关资源
最近更新 更多