Create credentials with scopes

Create credentials with Drive and BigQuery API scopes.

Code sample

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.

GoogleCredentialscredentials=ServiceAccountCredentials.getApplicationDefault().createScoped(ImmutableSet.of("https://www.googleapis.com/auth/bigquery","https://www.googleapis.com/auth/drive"));// Initialize client that will be used to send requests. This client only needs to be created// once, and can be reused for multiple requests.BigQuerybigquery=BigQueryOptions.newBuilder().setCredentials(credentials).build().getService();

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.cloudimportbigqueryimportgoogle.auth# Create credentials with Drive & BigQuery API scopes.# Both APIs must be enabled for your project before running this code.## If you are using credentials from gcloud, you must authorize the# application first with the following command:## gcloud auth application-default login \#   --scopes=https://www.googleapis.com/auth/drive,https://www.googleapis.com/auth/cloud-platformcredentials,project=google.auth.default(scopes=["https://www.googleapis.com/auth/drive","https://www.googleapis.com/auth/cloud-platform",])# Construct a BigQuery client object.client=bigquery.Client(credentials=credentials,project=project)

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.