【问题标题】:BigQuery: Permission denied while getting Drive credentials - Unable to resolve the errorBigQuery:获取云端硬盘凭据时权限被拒绝 - 无法解决错误
【发布时间】:2021-09-04 22:17:07
【问题描述】:

我希望就遇到的这个错误代码得到一些帮助。

上下文

  • 我工作的公司使用 GSUITE 产品。
  • 我的团队有自己的云项目设置。
  • Google 云端硬盘不是“个人”云端硬盘。
  • 我们利用 Airflow 在 每天/每周/每月。

我已遵循这些解决方案

Access Denied: Permission denied while getting Drive credentials

"Encountered an error while globbing file pattern" error when using BigQuery API w/ Google Sheets

并且还引用了 https://cloud.google.com/bigquery/external-data-drive#python_3

问题

云作曲家:v 1.12.0

我最近设置了一个外部 Bigquery 表,用于读取 Google 表格中的标签。由于 Drive 的访问限制,我的 Airflow DAG 未能完成。 我已将以下内容添加到 Airflow 连接范围:

airflow scopes

并且还将服务帐户电子邮件地址添加到该表通过 Share 引用的 Google 表格中。我还将服务帐户 IAM 角色更新为 BigQuery 管理员。执行这些步骤后,我仍然收到错误 BigQuery: Permission denied while getting Drive credentials。


问题2

根据上述情况,我发现在本地进行故障排除更容易,因此我在我的机器上创建了一个 VENV,因为它是我最舒服的故障排除位置。目标是简单地查询读取 Google 工作表的 Bigquery 表。但是,按照上述相同的步骤进行操作后,我仍然无法使其正常工作。

我的本​​地代码:

import dotenv
import pandas as pd
from google.cloud import bigquery
import google.auth

def run_BigQuery_table(sql):
    dotenv.load_dotenv()
    credentials, project = google.auth.default(
        scopes=[
            "https://www.googleapis.com/auth/cloud-platform",
            "https://www.googleapis.com/auth/drive",
            "https://www.googleapis.com/auth/bigquery",
        ]
    )
    bigquery.Client(project, credentials)
    output = pd.read_gbq(sql, project_id=project, dialect='standard')
    return output

script_variable = "SELECT * FROM `X` LIMIT 10"

bq_output = run_BigQuery_table(script_variable)
print(bq_output)

我的错误

提出自我._异常 google.api_core.exceptions.Forbidden: 403 Access Denied: BigQuery BigQuery: Permission denied > while getting Drive credentials.

引发 GenericGBQException("原因:{0}".format(ex)) pandas_gbq.gbq.GenericGBQException:原因:403 访问被拒绝:BigQuery BigQuery:权限 > 在获取云端硬盘凭据时被拒绝。

有人可以帮忙吗?

干杯

【问题讨论】:

    标签: python google-cloud-platform google-bigquery airflow


    【解决方案1】:

    所以一位同事建议我探索默认的 pandas_gbq 凭据,因为这可能会使用默认凭据来访问数据。

    事实证明,它奏效了。

    您可以按照以下步骤手动设置 pandas-gbq 凭据: https://pandas-gbq.readthedocs.io/en/latest/howto/authentication.html https://pandas-gbq.readthedocs.io/en/latest/api.html#pandas_gbq.Context.credentials

    我只是在我的代码中添加了以下内容

    pdgbq.context.credentials = credentials
    

    最终输出:

    import dotenv
    import pandas as pd
    from google.cloud import bigquery
    import google.auth
    import pandas_gbq as pdgbq
    
    
    def run_BigQuery_table(sql):
        dotenv.load_dotenv()
        credentials, project = google.auth.default(
            scopes=[
                "https://www.googleapis.com/auth/cloud-platform",
                "https://www.googleapis.com/auth/drive",
                "https://www.googleapis.com/auth/bigquery",
            ]
        )
        pdgbq.context.credentials = credentials
        bigquery.Client(project, credentials)
        output = pd.read_gbq(sql, project_id=project, dialect='standard')
        return output
    
    script_variable4 = "SELECT * FROM `X` LIMIT 10"
    
    bq_output = run_BigQuery_table(script_variable3)
    print(bq_output)
    

    【讨论】:

      【解决方案2】:

      我经常遇到这些错误,绝大多数都是通过创建和共享服务帐户解决的。但是,我最近遇到了一个案例,我们的 gsuite 管理员更新了安全设置,这样只有我们的员工才能访问 gsuite 相关的东西(电子表格、存储等)。这是为了填补安全漏洞,但这样做会阻止任何没有 @ourcompany.com 的电子邮件地址或服务帐户使用 BigQuery。

      我建议您探索您公司的 gsuite 设置,看看是否阻止了外部访问。我不能说这是你的情况的解决方案,但它适合我,所以值得一试..

      【讨论】:

      • 感谢您的建议,我们可以共享没有@ourcompany.com 的地址,但在共享之前会弹出警告/通知。
      猜你喜欢
      • 2021-06-28
      • 1970-01-01
      • 2021-08-27
      • 2017-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-25
      • 1970-01-01
      相关资源
      最近更新 更多