Authenticating With a User Account for Installed Applications

This guide explains how to authenticate using user accounts for access to the Google BigQuery API when your application is installed onto users' machines.

A user credential can be used to ensure the application accesses only BigQuery tables that are available to the end user. A user credential can run queries against only the end user's Cloud Platform project rather than the application's project, meaning the user is billed for queries instead of the application.

Before you begin

  1. Create a new Google Cloud Platform project representing your installed application.
  2. Install the BigQuery client libraries.
  3. Install additional libraries.


    Install the oauthlib integration for Google Auth.
    pip install --upgrade google-auth-oauthlib

Setting up your client credentials

  1. Go to the API credentials page in the Cloud Platform Console.
  2. Fill out the required fields on the OAuth consent screen.
  3. On the credentials page, click the Create credentials button.

    Choose OAuth client ID.

  4. Select Other as the application type, and then click Create.
  5. Download the credentials by clicking the Download JSON button.

    Download JSON

    Save the credentials file to client_secrets.json. This file must be distributed with your application.

Authenticating and calling the API

  1. Use the client credentials to perform the OAuth 2.0 flow.


    def authenticate_and_query(project, query, launch_browser=True):
        from google_auth_oauthlib import flow
        appflow = flow.InstalledAppFlow.from_client_secrets_file(
        if launch_browser:
        run_query(appflow.credentials, project, query)
  2. Use the authenticated credentials to connect to the BigQuery API.


    def run_query(credentials, project, query):
        from import bigquery
        client = bigquery.Client(project=project, credentials=credentials)
        query_job = client.query(query)
        # Print the results.
        for row in query_job.result():  # Wait for the job to complete.

When you run the sample code, it will launch a browser requesting access to the project associated with the client secrets. The resulting credentials can then be used to access the user's BigQuery resources, because the sample requested the BigQuery scope.

What's next

  1. Learn about other ways to authenticate your application to access the BigQuery API.
  2. Learn about authentication with end user credentials for all Google Cloud Platform APIs.
¿Te sirvió esta página? Envíanos tu opinión:

Enviar comentarios sobre…

¿Necesitas ayuda? Visita nuestra página de asistencia.