尝试使用python客户端访问BigQuery时出现身份验证错误

时间:2018-10-15 23:14:40

标签: python authentication google-cloud-platform google-bigquery

我正在尝试通过Python客户端查询BigQuery数据集。

我有一个启用了结算功能的项目,一个服务帐户已按照此处的指示分配了BigQuery管理员角色:https://cloud.google.com/bigquery/docs/quickstarts/quickstart-client-libraries

这是我正在尝试的代码段

from google.cloud import bigquery
jsonPath = "/Users/xyz/Downloads/serviceaccount.json"
client = bigquery.Client.from_service_account_json(jsonPath)
query_job = client.query("""
    SELECT
    CONCAT(
    'https://stackoverflow.com/questions/',
    CAST(id as STRING)) as url,
    view_count
    FROM `bigquery-public-data.stackoverflow.posts_questions`
    WHERE tags like '%google-bigquery%'
    ORDER BY view_count DESC
    LIMIT 10""")

results = query_job.result() 

client.query调用导致此错误:

Traceback (most recent call last):
  File "<stdin>", line 10, in <module>
  File "/Users/xyz/Library/Python/2.7/lib/python/site- 
packages/google/cloud/bigquery/client.py", line 1254, in query
    query_job._begin(retry=retry)
  File "/Users/xyz/Library/Python/2.7/lib/python/site- 
packages/google/cloud/bigquery/job.py", line 552, in _begin
    method='POST', path=path, data=self._build_resource())
  File "/Users/xyz/Library/Python/2.7/lib/python/site- 
packages/google/cloud/bigquery/client.py", line 336, in _call_api
    return call()
  File "/Users/xyz/Library/Python/2.7/lib/python/site- 
packages/google/api_core/retry.py", line 260, in retry_wrapped_func
    on_error=on_error,
   File "/Users/xyz/Library/Python/2.7/lib/python/site- 
packages/google/api_core/retry.py", line 177, in retry_target
    return target()
  File "/Users/xyz/Library/Python/2.7/lib/python/site- 
packages/google/cloud/_http.py", line 293, in api_request
    raise exceptions.from_http_response(response)
google.api_core.exceptions.Unauthorized: 401 POST . 
https://www.googleapis.com/bigquery/v2/projects/xyzproject/jobs: HTTP 
Basic Authentication is not supported for this API

关于如何解决此身份验证故障的任何指示?非常感谢

python -V 2.7.10 我已经安装了最新的Google云库

1 个答案:

答案 0 :(得分:1)

我带走了您的代码,添加了用于打印查询结果的语句,并在Python 2.7.15和Python 3.6.1下都运行了该程序,

这使我相信您有一个过时的库。 Google Python SDK使用了google-auth,urllib3等。请检查您是否安装了最新版本。

此外,我将更新您的Python版本。 2.7.10于2015年发布。

在命令提示符处:

if (!WebUtils.isEmpty(appAppService.getByAppid(id))) {
        LOGGER.info(":bind app info");
        errorNum++;
    }
    if (!WebUtils.isEmpty(appDownloadCensusService.getByAppid(id))) {
        LOGGER.info(":bind bi info");
        errorNum++;
    }
    if (!WebUtils.isEmpty(appManuCensusService.getByAppid(id))) {
        LOGGER.info(":bind download info");
        errorNum++;
    }
    if (!WebUtils.isEmpty(appOrderService.getByAppid(id))) {
        LOGGER.info(":bind order info");
        errorNum++;
    }
    if (!WebUtils.isEmpty(appShareService.getByAppid(id))) {
        LOGGER.info(":bind share info");
        errorNum++;
    }
    ......

然后使用grep或您喜欢的编辑器检查系统上已安装的版本。

我的Python 2.x环境正在使用:

  • google-api-core == 1.4.1
  • google-auth == 1.5.1
  • google-cloud-bigquery == 1.6.0
  • google-cloud-core == 0.28.1
  • google-resumable-media == 0.3.1
  • googleapis-common-protos == 1.5.3
  • urllib3 == 1.22

示例输出:

pip freeze > python_modules.list