SupimpaAllTheWay
SupimpaAllTheWay

Reputation: 1458

Google App Engine - 401 when requesting BigQuery API

I saw some similar questions but none of them actually gives a concrete solution to this problem.

The message I'm getting is this one:

HttpError 401 when requesting https://www.googleapis.com/bigquery/v2/projects/project/queries?alt=json returned "Invalid Credentials"

Code that connects to BigQuery:

   appname = get_application_id()                                                                                                                                                  
       project_id = appname                                                                                                                                                            
       # Grab the application's default credentials from the environment.                                                                                                              
       credentials = GoogleCredentials.get_application_default()                                                                                                                       
       # Construct the service object for interacting with the BigQuery API.                                                                                                           
       bigquery_service = build('bigquery', 'v2', credentials=credentials)                                                                                                             
       query_request = bigquery_service.jobs()     



       query_data = {                                                                                                                                                                  
              'query': query,                                                                                                                                                             
              'useLegacySql': use_legacy_sql                                                                                                                                              
          }                                                                                                                                                                               

       query_response = query_request.query(                                                                                                                                           
              projectId=project_id,                                                                                                                                                       
              body=query_data).execute()  

What could be the cause(s) for this problem?

Upvotes: 0

Views: 1274

Answers (1)

Graham Polley
Graham Polley

Reputation: 14781

Make sure:

  1. You've created a service account with the correct IAM roles assigned.
  2. Exported the environment variable GOOGLE_APPLICATION_CREDENTIALS to point to the file created in step 1.

Then it will work. More details can be found here.

Upvotes: 1

Related Questions