【问题标题】:Query data from Google Sheets-based table in BigQuery via API using service account使用服务帐户通过 API 从 BigQuery 中基于 Google Sheets 的表中查询数据
【发布时间】:2017-11-22 22:04:00
【问题描述】:

我可以使用服务帐号从原生 BigQuery 表中获取数据。

但是,当我尝试使用同一服务帐户从 BigQuery 中基于 Google 表格的表中 select 时遇到错误。

from google.cloud import bigquery

client = bigquery.Client.from_service_account_json(
    json_credentials_path='creds.json',
    project='xxx',
)

# this works fine
print('test basic query: select 1')
job = client.run_sync_query('select 1')
job.run()
print('results:', list(job.fetch_data()))
print('-'*50)

# this breaks
print('attempting to fetch from sheets-based BQ table')
job2 = client.run_sync_query('select * from testing.asdf')
job2.run()

输出:

⚡  ~/Desktop ⚡  python3 bq_test.py
test basic query: select 1
results: [(1,)]
--------------------------------------------------
attempting to fetch from sheets-based BQ table
Traceback (most recent call last):
  File "bq_test.py", line 16, in <module>
    job2.run()
  File "/usr/local/lib/python3.6/site-packages/google/cloud/bigquery/query.py", line 381, in run
    method='POST', path=path, data=self._build_resource())
  File "/usr/local/lib/python3.6/site-packages/google/cloud/_http.py", line 293, in api_request
    raise exceptions.from_http_response(response)
google.cloud.exceptions.Forbidden: 403 POST https://www.googleapis.com/bigquery/v2/projects/warby-parker-1348/queries: Access Denied: BigQuery BigQuery: No OAuth token with Google Drive scope was found.

我尝试使用oauth2client.service_account.ServiceAccountCredentials 显式定义scopes,包括drive 的范围,但尝试这样做时出现以下错误:

ValueError: This library only supports credentials from google-auth-library-python. See https://google-cloud-python.readthedocs.io/en/latest/core/auth.html for help on authentication with this library.

我的理解是,身份验证现在是通过 IAM 处理的,但我看不到任何角色可应用于此服务帐户与驱动器有任何关系。

如何使用 BigQuery python 客户端从工作表支持的表中进行选择?

【问题讨论】:

  • 你有没有想过这个问题?我遇到了同样的问题。

标签: google-bigquery google-oauth google-cloud-iam


【解决方案1】:

我遇到了同样的问题并想出了如何解决它。

在探索google.cloud.bigquery.Client 类时,有一个全局变量元组SCOPE 不会被任何参数或任何Credentials 对象更新,将其默认值持久保存到使用它的类。

要解决此问题,您只需将新的范围 URL 添加到 google.cloud.bigquery.Client.SCOPE 元组即可。

在以下代码中,我将 Google Drive 范围添加到其中:

from google.cloud import bigquery

#Add any scopes needed onto this scopes tuple.
scopes = (
     'https://www.googleapis.com/auth/drive'
)

bigquery.Client.SCOPE+=scopes

client = bigquery.Client.from_service_account_json(
    json_credentials_path='/path/to/your/credentials.json',
    project='your_project_name',
)

使用上面的代码,您将能够在 BigQuery 中从基于表格的表格中查询数据。

希望对你有帮助!

【讨论】:

  • 天哪,这真是救命稻草。太感谢了。我尝试了我发现的其他修复,但它们只是给出了不同的错误。
  • 这在 2022 年仍然是救命稻草
【解决方案2】:

我认为您在进行身份验证时需要传递 gdrive 的范围是对的。范围在此处传递 https://github.com/GoogleCloudPlatform/google-cloud-python/blob/master/core/google/cloud/client.py#L126 ,BigQuery 客户端似乎缺少这些范围 https://github.com/GoogleCloudPlatform/google-cloud-python/blob/master/bigquery/google/cloud/bigquery/client.py#L117 。我建议在 github 上询问,作为一种解决方法,您可以尝试覆盖包括 gdrive 范围在内的客户端凭据,但您需要使用来自 GoogleCloudPlatform/google-auth-library-python 的 google.auth.credentials 而不是 oauth2client,作为错误消息建议。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-17
    • 2020-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-08
    相关资源
    最近更新 更多