【问题标题】:Authentication error when trying to access BigQuery using python client尝试使用 python 客户端访问 BigQuery 时出现身份验证错误
【发布时间】:2019-03-20 10:10:36
【问题描述】:

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

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

这是我正在尝试的代码 sn-p

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://*.com/questions/',
    CAST(id as STRING)) as url,
    view_count
    FROM `bigquery-public-data.*.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 我安装了最新的谷歌云库

【问题讨论】:

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


    【解决方案1】:

    我获取了您的代码,添加了语句来打印查询结果,并在 Python 2.7.15 和 Python 3.6.1 下运行了程序,并且运行良好。

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

    另外,我会更新你的 Python 版本。 2.7.10于2015年发布。

    在命令提示符处:

    pip freeze > python_modules.list
    

    然后使用 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

    样本输出:

    https://*.com/questions/13530967 : 44672 views
    https://*.com/questions/22879669 : 34745 views
    https://*.com/questions/13221978 : 31584 views
    https://*.com/questions/6607552 : 27736 views
    https://*.com/questions/16609219 : 26271 views
    https://*.com/questions/35159967 : 26258 views
    https://*.com/questions/10604135 : 25860 views
    https://*.com/questions/22004216 : 23496 views
    https://*.com/questions/10644993 : 22356 views
    https://*.com/questions/11647201 : 18547 views
    

    【讨论】:

    • 非常感谢您的详细回答。我根据您的笔记检查了我的环境并更新了所有内容,但是我仍然遇到此错误。我想知道我的 IP 连接到 bigquery 服务是否有任何问题,但我不知道如何解决。我在谷歌云上尝试了来自 VM 的代码,它在那里工作,所以我现在要使用它。再次感谢您,这是我第一次在这里提出问题,我很高兴得到答复。