Baptiste
Baptiste

Reputation: 133

Authentification issue to BigQuery API REST using Python

I would like to make a HTTP call to this resource : https://bigquery.googleapis.com/bigquery/v2/projects/{projectId}/jobs

As I read to the documentation I use an API key generated from my GCP project to be authenticated. So with requests I make a simple call like this:

import requests

params = {'key': 'MY_API_KEY'}
base_url = 'https://bigquery.googleapis.com'
project_id = 'MY_PROJECT_ID'
r = requests.get(f'{base_url}/bigquery/v2/projects/{project_id}/jobs', params=params)

Unfortunately it returns a response 401 and I can't figure out why.

Thanks a lot and have a nice day !

Update code after guillaume blaquiere reply :

from google.auth.transport.requests import AuthorizedSession
from google.oauth2 import service_account

base_url = 'https://bigquery.googleapis.com'
project_id = 'project_id'

credentials = service_account.Credentials.from_service_account_file(
    'service_account.json',
    scopes=['https://www.googleapis.com/auth/bigquery',
            'https://www.googleapis.com/auth/cloud-platform'],
)

authed_session = AuthorizedSession(credentials)
response = authed_session.request('GET', f'{base_url}/bigquery/v2/projects/{project_id}/jobs')
print(response.json())

# this returns : {'etag': 'tAZvk1k2f2GY8yHaQF7how==', 'kind': 'bigquery#jobList'}

Upvotes: 0

Views: 976

Answers (1)

guillaume blaquiere
guillaume blaquiere

Reputation: 75765

The API Key no longer works for a large number of Google API. Only some legacy continue to accept an API key.

Now, you need an authenticated request. You can find exemple in the google-auth python library documentation. Look at Refresh and Authorized_session.

Don't hesitate to comment if you need help about the credential obtention, I can also help you on this.

EDIT

When you perform the request, it's, by default, only on the current user. In your case, it's the service account when you use the Python code, and your User account when you use the API Explorer (the swagger like in the Google Documentation).

In your case, I guess that your service account has never performed a job (query or load job) and thus, there is no entry for it.

According with the documentation, is you want to see all the user jobs, you have to add the param ?allUsers=true at the end of your URL

response = authed_session.request('GET', f'{base_url}/bigquery/v2/projects/{project_id}/jobs?allUsers=true')

Upvotes: 2

Related Questions