BigQuery:获取云端硬盘凭据时权限被拒绝 - 无法解决错误

时间:2021-06-21 08:15:12

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

我希望能得到一些关于我遇到的错误代码的帮助。

上下文

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

我遵循了这些解决方案

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

并且还将服务帐户电子邮件地址添加到表格通过共享引用的 Google 表格中。我还将服务帐户 IAM 角色更新为 BigQuery 管理员。按照这些步骤操作后,我仍然收到错误 BigQuery:获取云端硬盘凭据时权限被拒绝。


问题 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)

我的错误

<块引用>

提高 self._exception google.api_core.exceptions.Forbidden: 403 Access Denied: BigQuery BigQuery: Permission denied > 在获取云端硬盘凭据时。

<块引用>

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

有人可以帮忙吗?

干杯

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分:0)

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

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