【问题标题】:Querying federated tables (Google Drive) via BigQuery python api通过 BigQuery python api 查询联合表(Google Drive)
【发布时间】:2018-07-18 06:25:27
【问题描述】:

(这里有很多类似的帖子,但不幸的是,我在这里或 Goolge 上的任何地方都找不到我的错误的答案)

我正在尝试在 BigQuery 中查询一个联合表,该表指向云端硬盘中的电子表格。

我已运行以下命令为 gcloud 创建默认应用程序凭据:

$ gcloud auth application-default login

但这不包括 Drive 进入范围,所以我收到以下错误消息(这是有道理的):Forbidden: 403 Access Denied: BigQuery BigQuery: No OAuth token with Google Drive scope was found。

然后我尝试使用明确的 Drive 范围进行身份验证:

$ gcloud auth application-default login --scopes=https://www.googleapis.com/auth/drive,https://www.googleapis.com/auth/cloud-platform,https://www.googleapis.com/auth/bigquery

之后,当我尝试使用 bigquery python api 时出现以下错误:

"Forbidden: 403 Access Denied: BigQuery BigQuery: Access Not Configured. Drive API has not been used in project 764086051850 before or it is disabled. 通过访问 https://console.developers.google.com/apis/api/drive.googleapis.com/overview?project=764086051850 启用它,然后重试。如果您最近启用了此 API,请稍候几分钟后该操作会传播到我们的系统并重试。”

上述项目编号在我们的组织中不存在,并且提供的链接指向一个页面,该页面显示: API“drive.googleapis.com”不存在或您无权访问它

默认项目肯定启用了Drive API,因此错误消息没有多大意义。我还可以使用bq query_string 命令从终端查询表。

我目前不知道如何进一步调试,有人建议吗?

配置:
谷歌云 SDK 187.0.0
Python 2.7
谷歌云 0.27.0
谷歌云大查询 0.29.0

【问题讨论】:

  • 您是否完成了此处概述的 3 个步骤:stackoverflow.com/questions/40731823/…
  • @GrahamPolley 感谢您的回答!我没有使用服务帐户,而是通过我自己的帐户登录。但是,是的,Drive API 已启用,我可以访问电子表格(我可以通过 $ bq query 查询表格)。如上所示,请求驱动范围。还是$ gcloud auth application-default login 只应该与服务帐户一起使用?
  • 正如我上面所描述的,错误消息提到了一个甚至不属于我们组织的项目。更不用说它不是我在$ gcloud init上设置的默认项目
  • 不太明白。您是否在 python 脚本中使用了gcloud init 创建的凭据?
  • 我在我的 python 脚本中使用应用程序默认凭据。喜欢这里:from google.cloud import bigquery client = bigquery.Client() query = "SELECT * FROM tablename LIMIT 5" query_job = client.query(query) rows = query_job.result() for row in rows: print(row.name)

标签: python google-bigquery gcloud


【解决方案1】:

使用默认凭据时可能会有issues。但是,您可以使用服务帐户,将凭据保存在 JSON 文件中并添加必要的范围。我做了一个快速测试,这段代码对我有用:

from google.cloud import bigquery
from google.oauth2.service_account import Credentials

scopes = (
        'https://www.googleapis.com/auth/bigquery',
        'https://www.googleapis.com/auth/cloud-platform',
        'https://www.googleapis.com/auth/drive'
)
credentials = Credentials.from_service_account_file('/path/to/credentials.json')
credentials = credentials.with_scopes(scopes)
client = bigquery.Client(credentials=credentials)

query = "SELECT * FROM dataset.federated_table LIMIT 5"
query_job = client.query(query)
rows = query_job.result()
for row in rows: print(row)

如果您收到 404 not found 错误是因为您需要与服务帐户共享电子表格(查看权限)

【讨论】:

  • 截至今天,2021 年 3 月,我尝试并发现不需要 scope 'https://www.googleapis.com/auth/cloud-platform'
猜你喜欢
  • 1970-01-01
  • 2021-09-07
  • 1970-01-01
  • 2021-01-21
  • 2021-12-30
  • 1970-01-01
  • 2018-05-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多