Login Required error when trying to query Google BigQuery using Python

I would like to access BigQuery data from my local Linux machine using Python. The code from google help https://cloud.google.com/bigquery/authorization#service-accounts-server works great giving me a list of datasets. But the request is sent through the service library

SELECT id, name FROM [test_articles.countries] LIMIT 100

      

with the message "Login Required":

googleapiclient.errors.HttpError: <HttpError 401 when requesting https://www.googleapis.com/bigquery/v2/projects/myproject/queries?alt=json returned "Login Required">

      

The query works fine in BigQuery Web UI. For auth I am using the "Google API Service Account" from the Google Development Console with "Can edit" permission.

Here is all the code

import httplib2

from apiclient.discovery import build
from oauth2client.client import SignedJwtAssertionCredentials

# REPLACE WITH YOUR Project ID
PROJECT_ID = "xxxxxx"
# REPLACE WITH THE SERVICE ACCOUNT EMAIL FROM GOOGLE DEV CONSOLE
SERVICE_ACCOUNT_EMAIL = 'xxxxxxx@developer.gserviceaccount.com'

f = file('xxxxx.p12', 'rb')
key = f.read()
f.close()

credentials = SignedJwtAssertionCredentials(
    SERVICE_ACCOUNT_EMAIL,
    key,
    scope='https://www.googleapis.com/auth/bigquery')

http = httplib2.Http()
http = credentials.authorize(http)

service = build('bigquery', 'v2')
datasets = service.datasets()
response = datasets.list(projectId=PROJECT_ID).execute(http)

print('Dataset list:\n')
for dataset in response['datasets']:
  print("%s\n" % dataset['id'])


# SELECT data

query_data = {'query':'
    SELECT id, name FROM [test_articles.countries] LIMIT 100;
'}
query_request = service.jobs()
query_response = query_request.query(projectId=PROJECT_ID, 
    body=query_data).execute()
pp.pprint(query_response)

      

+3


source to share


2 answers


Instead:

service = build('bigquery', 'v2')
datasets = service.datasets()
response = datasets.list(projectId=PROJECT_ID).execute(http)

      

Try:



service = build('bigquery', 'v2', http=http)
datasets = service.datasets()
response = datasets.list(projectId=PROJECT_ID).execute()

      

(use an authenticated HTTP connection when creating the service)

+1


source


Be sure to login to your gcloud account via terminal:



gcloud auth application-default login

      

+1


source







All Articles