使用带有用户身份验证的Python BigQuery API时出错



使用最终用户身份验证从Python查询BigQuery时出错

它在服务帐户身份验证中成功工作,但在最终用户身份验证中失败
我基本上遵循这些说明https://cloud.google.com/docs/authentication/end-user

错误消息为:
ProjectId and DatasetId must be non-empty
我被难住了。使用服务帐户身份验证会返回预期的数据,因此这似乎是一个与身份验证相关的问题,但身份验证步骤似乎是成功的。
详细信息

from google_auth_oauthlib import flow
from google.cloud import bigquery
appflow = flow.InstalledAppFlow.from_client_secrets_file(
"client_secrets.json", scopes=["https://www.googleapis.com/auth/bigquery"])
appflow.run_local_server()
credentials = appflow.credentials
client = bigquery.Client(project='MyProject', credentials=credentials)
query_string = """SELECT name, SUM(number) as total
FROM `bigquery-public-data.usa_names.usa_1910_current`
WHERE name = 'William'
GROUP BY name;
"""
query_job = client.query(query_string)
for row in query_job.result(): 
print("{}: {}".format(row["name"], row["total"]))

给出以下错误:

Please visit this URL to authorize this application: https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=...
Traceback (most recent call last):
File "C:pythontestbqtest3.py", line 15, in <module>
query_job = client.query(query_string)
File "C:UsersmeAppDataLocalProgramsPythonPython310libsite-packagesgooglecloudbigqueryclient.py", line 3331, in query
return _job_helpers.query_jobs_insert(
File "C:UsersmeAppDataLocalProgramsPythonPython310libsite-packagesgooglecloudbigquery_job_helpers.py", line 114, in query_jobs_insert
future = do_query()
File "C:UsersmeAppDataLocalProgramsPythonPython310libsite-packagesgooglecloudbigquery_job_helpers.py", line 91, in do_query
query_job._begin(retry=retry, timeout=timeout)
File "C:UsersmeAppDataLocalProgramsPythonPython310libsite-packagesgooglecloudbigqueryjobquery.py", line 1298, in _begin
super(QueryJob, self)._begin(client=client, retry=retry, timeout=timeout)
File "C:UsersmeAppDataLocalProgramsPythonPython310libsite-packagesgooglecloudbigqueryjobbase.py", line 510, in _begin
api_response = client._call_api(
File "C:UsersmeAppDataLocalProgramsPythonPython310libsite-packagesgooglecloudbigqueryclient.py", line 756, in _call_api
return call()
File "C:UsersmeAppDataLocalProgramsPythonPython310libsite-packagesgoogleapi_coreretry.py", line 283, in retry_wrapped_func
return retry_target(
File "C:UsersmeAppDataLocalProgramsPythonPython310libsite-packagesgoogleapi_coreretry.py", line 190, in retry_target
return target()
File "C:UsersmeAppDataLocalProgramsPythonPython310libsite-packagesgooglecloud_http__init__.py", line 494, in api_request
raise exceptions.from_http_response(response)
google.api_core.exceptions.BadRequest: 400 POST https://bigquery.googleapis.com/bigquery/v2/projects/MyProject/jobs?prettyPrint=false: ProjectId and DatasetId must be non-empty
Location: None
Job ID: 9eba1ce9-971a-4495-825a-728aed28fc98

请将python env添加到脚本中,如下所示(第一行)。

#!/usr/bin/env python
from google_auth_oauthlib import flow
from google.cloud import bigquery
appflow = flow.InstalledAppFlow.from_client_secrets_file("client_secrets.json", scopes=["https://www.googleapis.com/auth/bigquery"])
appflow.run_local_server()
credentials = appflow.credentials
client = bigquery.Client(project='MyProject', credentials=credentials)
query_job = client.query(
"""
SELECT name, SUM(number) as total
FROM `bigquery-public-data.usa_names.usa_1910_current`
WHERE name = 'William'
GROUP BY name;
"""
)
for row in query_job.result(): 
print("{}: {}".format(row["name"], row["total"]))

最新更新