Create a client with a service account key file

Create a BigQuery client using a service account key file.

Code sample

C#

Before trying this sample, follow theC# setup instructions in theBigQuery quickstart using client libraries. For more information, see theBigQueryC# API reference documentation.

To authenticate to BigQuery, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.

varcredentials=GoogleCredential.FromFile(jsonPath);varclient=BigQueryClient.Create(projectId,credentials);

Java

Before trying this sample, follow theJava setup instructions in theBigQuery quickstart using client libraries. For more information, see theBigQueryJava API reference documentation.

To authenticate to BigQuery, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.

publicstaticvoidexplicit()throwsIOException{// TODO(developer): Replace these variables before running the sample.StringprojectId="MY_PROJECT_ID";FilecredentialsPath=newFile("path/to/your/service_account.json");// Load credentials from JSON key file. If you can't set the GOOGLE_APPLICATION_CREDENTIALS// environment variable, you can explicitly load the credentials file to construct the// credentials.GoogleCredentialscredentials;try(FileInputStreamserviceAccountStream=newFileInputStream(credentialsPath)){credentials=ServiceAccountCredentials.fromStream(serviceAccountStream);}// Instantiate a client.BigQuerybigquery=BigQueryOptions.newBuilder().setCredentials(credentials).setProjectId(projectId).build().getService();// Use the client.System.out.println("Datasets:");for(Datasetdataset:bigquery.listDatasets().iterateAll()){System.out.printf("%s%n",dataset.getDatasetId().getDataset());}}

Node.js

Before trying this sample, follow theNode.js setup instructions in theBigQuery quickstart using client libraries. For more information, see theBigQueryNode.js API reference documentation.

To authenticate to BigQuery, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.

// Create a BigQuery client explicitly using service account credentials.// by specifying the private key file.const{BigQuery}=require('@google-cloud/bigquery');constoptions={keyFilename:'path/to/service_account.json',projectId:'my_project',};constbigquery=newBigQuery(options);

Python

Before trying this sample, follow thePython setup instructions in theBigQuery quickstart using client libraries. For more information, see theBigQueryPython API reference documentation.

To authenticate to BigQuery, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.

fromgoogle.cloudimportbigqueryfromgoogle.oauth2importservice_account# TODO(developer): Set key_path to the path to the service account key#                  file.# key_path = "path/to/service_account.json"credentials=service_account.Credentials.from_service_account_file(key_path,scopes=["https://www.googleapis.com/auth/cloud-platform"],)# Alternatively, use service_account.Credentials.from_service_account_info()# to set credentials directly via a json object rather than set a filepath# TODO(developer): Set key_json to the content of the service account key file.# credentials = service_account.Credentials.from_service_account_info(key_json)client=bigquery.Client(credentials=credentials,project=credentials.project_id,)

What's next

To search and filter code samples for other Google Cloud products, see theGoogle Cloud sample browser.

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.