Google bigquery 使用服务帐户通过API从BigQuery中基于Google Sheets的表中查询数据

Google bigquery 使用服务帐户通过API从BigQuery中基于Google Sheets的表中查询数据,google-bigquery,google-oauth,google-cloud-iam,Google Bigquery,Google Oauth,Google Cloud Iam,我可以使用服务帐户从本机BigQuery表中获取数据 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_syn

我可以使用服务帐户从本机BigQuery表中获取数据

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()
但是,我在尝试使用相同的服务帐户从BigQuery中基于Google Sheets的表中选择时遇到了一个错误

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.
我的理解是,身份验证现在是通过IAM处理的,但我看不到任何应用于此服务帐户的角色与驱动器有任何关系


如何使用BigQuery python客户端从表中进行选择?

我认为您在进行身份验证时需要传递gdrive的作用域是正确的。作用域在这里传递,看起来BigQuery客户端缺少这些作用域。我建议在github上询问,并且作为一种解决方法,您可以尝试覆盖客户端凭据,包括gdrive scope,但您需要使用google CloudPlatform/google auth library python中的google.auth.credentials,而不是oauth2client,正如错误消息所示。

我遇到了同样的问题,并找到了解决方法

在探索类时,有一个全局变量元组不会被任何参数或任何
凭证
对象更新,它会将其默认值保留到使用后的类中

要解决这个问题,只需向
google.cloud.bigquery.Client.scope
元组添加一个新的作用域URL即可

在以下代码中,我将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中从基于图纸的表中查询数据


希望有帮助

你有没有想过?我也遇到了同样的问题。哦,天哪,这真是个救命稻草。非常感谢你。我尝试了我发现的其他修复,但它们只是给出了不同的错误。