Dae*_*ark 11 python google-bigquery google-cloud-platform airflow
我希望针对我遇到的这个错误代码获得一些帮助。
语境:
我已遵循这些解决方案
使用 BigQuery API 和 Google Sheets 时出现“匹配文件模式时遇到错误”错误
还引用了 https://cloud.google.com/bigquery/external-data-drive#python_3
问题
云作曲家:v 1.12.0
我最近设置了一个外部 Bigquery 表,用于读取 Google Sheet 中的选项卡。由于对 Drive 的访问限制,我的 Airflow DAG 一直无法完成。我已将以下内容添加到 Airflow 连接范围中:
还将服务帐户电子邮件地址添加到该表通过共享引用的 Google 表格中。我还将服务帐户 IAM 角色更新为 BigQuery admin。执行这些步骤后,我仍然收到错误 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)
Run Code Online (Sandbox Code Playgroud)
我的错误:
引发 self._exception google.api_core.exceptions.Forbidden: 403 访问被拒绝: BigQuery BigQuery: 获取云端硬盘凭据时权限被拒绝。
raise GenericGBQException("原因:{0}".format(ex)) pandas_gbq.gbq.GenericGBQException:原因:403 访问被拒绝:BigQuery BigQuery:获取云端硬盘凭据时权限 > 被拒绝。
有人能帮忙吗?
干杯
因此,一位同事建议我探索默认的 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
Run Code Online (Sandbox Code Playgroud)
最终输出:
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)
Run Code Online (Sandbox Code Playgroud)
我经常遇到这些错误,绝大多数都是通过创建和共享服务帐户解决的。然而,我最近遇到了一个案例,我们的 gsuite 管理员更新了安全设置,以便只有我们的员工才能访问 gsuite 相关的内容(电子表格、存储等)。这是为了填补安全漏洞,但这样做时,任何没有 @ourcompany.com 的电子邮件地址或服务帐户都会被阻止使用 BigQuery。
我建议您探索公司的 gsuite 设置,看看外部访问是否被阻止。我不能说这是针对您的情况的解决方案,但它适合我,所以值得尝试。
| 归档时间: |
|
| 查看次数: |
27455 次 |
| 最近记录: |