Authenticate to BigQuery Stay organized with collections Save and categorize content based on your preferences.
This document describes how to authenticate to BigQuery programmatically. How you authenticate to BigQuery depends on the interface you use to access the API and the environment where your code is running.
For a short example of authenticating to BigQuery, seeGetting started with authentication.For more information about Google Cloud authentication, see theAuthentication methods.
API access
BigQuery supports programmatic access. You can access the API in the following ways:
Client libraries
TheBigQuery API client libraries provide high-level language support for authenticating to BigQuery programmatically. To authenticate calls to Google Cloud APIs, client libraries supportApplication Default Credentials (ADC);the libraries look for credentials in a set of defined locations and use those credentialsto authenticate requests to the API. With ADC, you can makecredentials available to your application in a variety of environments, such as localdevelopment or production, without needing to modify your application code.
Google Cloud CLI
When you use thegcloud CLI to access BigQuery, youlog in to the gcloud CLI with a user account, which provides the credentials used by the gcloud CLI commands.
If your organization's security policies prevent user accounts from having the required permissions, you can useservice account impersonation.
For more information, seeAuthenticate for using the gcloud CLI. For more information about using the gcloud CLI with BigQuery, seeInstall the gcloud CLI.
REST
You can authenticate tothe BigQuery API by using your gcloud CLI credentials or by usingApplication Default Credentials. For more information about authentication for REST requests, seeAuthenticate for using REST. For information about the types of credentials, seegcloud CLI credentials and ADC credentials.
Set up authentication for BigQuery
How you set up authentication depends on the environment where your code is running.
The following options for setting up authentication are the most commonly used. For more options and information about authentication, seeAuthentication methods.
For a local development environment
You can set up credentials for a local development environment in the following ways:
- User credentials for client libraries or third-party tools
- User credentials for REST requests from the command line
- Service account impersonation
Client libraries or third-party tools
Set upApplication Default Credentials (ADC) in your local environment:
Afterinstalling the Google Cloud CLI,initialize it by running the following command:
gcloudinit
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
If you're using a local shell, then create local authentication credentials for your user account:
gcloudauthapplication-defaultlogin
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
A sign-in screen appears. After you sign in, your credentials are stored in the local credential file used by ADC.
For more information about working with ADC in a local environment, seeSet up ADC for a local development environment.
REST requests from the command line
When you make a REST request from the command line, you can use your gcloud CLI credentials by includinggcloud auth print-access-token
as part of the command that sends the request.
The following example lists service accounts for the specified project. You can use the same pattern for any REST request.
Before using any of the request data, make the following replacements:
- PROJECT_ID: Your Google Cloud project ID.
To send your request, expand one of these options:
curl (Linux, macOS, or Cloud Shell)
Execute the following command:
curl -X GET \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://iam.googleapis.com/v1/projects/PROJECT_ID/serviceAccounts"
PowerShell (Windows)
Execute the following command:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://iam.googleapis.com/v1/projects/PROJECT_ID/serviceAccounts" | Select-Object -Expand Content
For more information about authenticating using REST and gRPC, seeAuthenticate for using REST. For information about the difference between your local ADC credentials and your gcloud CLI credentials, seegcloud CLI authentication configuration and ADC configuration.
Service account impersonation
In most cases, you can use your user credentials to authenticate from a local development environment. If that is not feasible, or if you need to test the permissions assigned to a service account, you can use service account impersonation. You must have theiam.serviceAccounts.getAccessToken
permission, which is included in theService Account Token Creator (roles/iam.serviceAccountTokenCreator
) IAM role.
You can set up the gcloud CLI to use service account impersonation by using thegcloud config set
command:
gcloudconfigsetauth/impersonate_service_accountSERVICE_ACCT_EMAIL
For select languages, you can use service account impersonation to create a local ADC file for use by client libraries. This approach is supported only for the Go, Java, Node.js, and Python client libraries—it is not supported for the other languages. To set up a local ADC file with service account impersonation, use the--impersonate-service-account
flag with thegcloud auth application-default login
command:
gcloudauthapplication-defaultlogin--impersonate-service-account=SERVICE_ACCT_EMAIL
For more information about service account impersonation, seeUse service account impersonation.
On Google Cloud
To authenticate a workload running on Google Cloud, you use the credentials of the service account attached to the compute resource where your code is running, such as a Compute Engine virtual machine (VM) instance. This approach is the preferred authentication method for code running on a Google Cloud compute resource.
For most services, you must attach the service account when you create the resource that will run your code; you cannot add or replace the service account later. Compute Engine is an exception—it lets you attach a service account to a VM instance at any time.
Use the gcloud CLI to create a service account and attach it to your resource:
Afterinstalling the Google Cloud CLI,initialize it by running the following command:
gcloudinit
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
Set up authentication:
Create the service account:
gcloudiamservice-accountscreateSERVICE_ACCOUNT_NAME
Replace
SERVICE_ACCOUNT_NAME
with a name for the service account.To provide access to your project and your resources, grant a role to the service account:
gcloudprojectsadd-iam-policy-bindingPROJECT_ID--member="serviceAccount:SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com"--role=ROLE
Replace the following:
SERVICE_ACCOUNT_NAME
: the name of the service accountPROJECT_ID
: the project ID where you created the service accountROLE
: the role to grant
--role
flag affects which resources the service account can access in your project. You can revoke these roles or grant additional roles later. In production environments, do not grant the Owner, Editor, or Viewer roles. Instead, grant apredefined role orcustom role that meets your needs.- To grant another role to the service account, run the command as you did in the previous step.
Grant the required role to the principal that will attach the service account to other resources.
gcloudiamservice-accountsadd-iam-policy-bindingSERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com--member="user:USER_EMAIL"--role=roles/iam.serviceAccountUser
Replace the following:
SERVICE_ACCOUNT_NAME
: the name of the service accountPROJECT_ID
: the project ID where you created the service accountUSER_EMAIL
: the email address for a Google Account
Create the resource that will run your code, and attach the service account to that resource. For example, if you use Compute Engine:
Create a Compute Engine instance. Configure the instance as follows:- Replace
INSTANCE_NAME
with your preferred instance name. - Set the
--zone
flag to thezone in which you want to create your instance. - Set the
--service-account
flag to the email address for the service account that you created.
gcloudcomputeinstancescreateINSTANCE_NAME --zone=ZONE --service-account=SERVICE_ACCOUNT_EMAIL
- Replace
For more information about authenticating to Google APIs, seeAuthentication methods.
On-premises or on a different cloud provider
The preferred method to set up authentication from outside of Google Cloud is to use workload identity federation. For more information, seeSet up ADC for on-premises or another cloud provider in the authentication documentation.
Access control for BigQuery
After you authenticate to BigQuery, you must be authorized to access Google Cloud resources. BigQuery uses Identity and Access Management (IAM) for authorization.
For more information about the roles for BigQuery, seeIntroduction to IAM in BigQuery. For more information about IAM and authorization, seeIAM overview.
What's next
- Get started with authentication.
- Authenticate installed apps with user accounts.
- Authenticate with JSON Web Tokens.
- Learn aboutGoogle Cloud authentication methods.
- See a list ofauthentication use cases.
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-07-02 UTC.