Anju Jha
Anju Jha

Reputation: 1

Authentication error when trying to access BigQuery using python client

I am trying to query a BigQuery dataset through a Python client.

I have a project with billing enabled, a service account with BigQuery admin role assigned as instructed here:https://cloud.google.com/bigquery/docs/quickstarts/quickstart-client-libraries

This is the code snippet I am trying

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() 

The client.query invocation results in this error:

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

Any pointers on what I can do to resolve this authentication hiccup? Thanks so much

python -V 2.7.10 I have latest google cloud libraries installed

Upvotes: 0

Views: 1264

Answers (1)

John Hanley
John Hanley

Reputation: 81356

I took your code, added statements to print the query results and ran the program under both Python 2.7.15 and under Python 3.6.1 and it worked just fine.

This leads me to believe that you have an out of date library. Google Python SDKs make use of google-auth, urllib3, etc. Check that you have the latest versions installed.

Also, I would update your version of Python. 2.7.10 was released in 2015.

At the command prompt:

pip freeze > python_modules.list

Then using grep or your favorite editor check the versions that you have installed on your system.

My Python 2.x environment is using:

  • 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

Sample output:

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

Upvotes: 1

Related Questions