【问题标题】:Error using Python BigQuery API with user authentication使用带有用户身份验证的 Python BigQuery API 时出错
【发布时间】:2022-06-27 18:10:03
【问题描述】:

我在使用最终用户身份验证从 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:\python\test\bqtest3.py", line 15, in <module>
    query_job = client.query(query_string)
  File "C:\Users\me\AppData\Local\Programs\Python\Python310\lib\site-packages\google\cloud\bigquery\client.py", line 3331, in query
    return _job_helpers.query_jobs_insert(
  File "C:\Users\me\AppData\Local\Programs\Python\Python310\lib\site-packages\google\cloud\bigquery\_job_helpers.py", line 114, in query_jobs_insert
    future = do_query()
  File "C:\Users\me\AppData\Local\Programs\Python\Python310\lib\site-packages\google\cloud\bigquery\_job_helpers.py", line 91, in do_query
    query_job._begin(retry=retry, timeout=timeout)
  File "C:\Users\me\AppData\Local\Programs\Python\Python310\lib\site-packages\google\cloud\bigquery\job\query.py", line 1298, in _begin
    super(QueryJob, self)._begin(client=client, retry=retry, timeout=timeout)
  File "C:\Users\me\AppData\Local\Programs\Python\Python310\lib\site-packages\google\cloud\bigquery\job\base.py", line 510, in _begin
    api_response = client._call_api(
  File "C:\Users\me\AppData\Local\Programs\Python\Python310\lib\site-packages\google\cloud\bigquery\client.py", line 756, in _call_api
    return call()
  File "C:\Users\me\AppData\Local\Programs\Python\Python310\lib\site-packages\google\api_core\retry.py", line 283, in retry_wrapped_func
    return retry_target(
  File "C:\Users\me\AppData\Local\Programs\Python\Python310\lib\site-packages\google\api_core\retry.py", line 190, in retry_target
    return target()
  File "C:\Users\me\AppData\Local\Programs\Python\Python310\lib\site-packages\google\cloud\_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

【问题讨论】:

    标签: google-bigquery google-authentication


    【解决方案1】:

    请像下面这样将python env添加到脚本中(以粗体标记)。

    #!/usr/bin/env python 从 google_auth_oauthlib 导入流程 从 google.cloud 导入 bigquery

    appflow = flow.InstalledAppFlow.from_client_secrets_file("client_secrets.json", scopes=["https://www.googleapis.com/auth/bigquery"]) appflow.run_local_server() 凭据 = appflow.credentials

    client = bigquery.Client(project='MyProject', credentials=credentials) query_job = client.query( """ 选择名称,总和(数字)作为总数 来自bigquery-public-data.usa_names.usa_1910_current WHERE 名称 = '威廉' 按名称分组; """ )

    对于 query_job.result() 中的行: print("{}: {}".format(row["name"], row["total"]))

    【讨论】:

      猜你喜欢
      • 2019-03-20
      • 1970-01-01
      • 2016-04-12
      • 2012-08-06
      • 1970-01-01
      • 2019-10-25
      • 1970-01-01
      • 2016-01-26
      • 1970-01-01
      相关资源
      最近更新 更多