Create credentials with scopes
Stay organized with collections
Save and categorize content based on your preferences.
Create credentials with Drive and BigQuery API scopes.
Code sample
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis guide demonstrates how to create credentials with both Drive and BigQuery API scopes for accessing Google Cloud services.\u003c/p\u003e\n"],["\u003cp\u003eThe provided code examples use Java and Python, and they require setting up Application Default Credentials for authentication to BigQuery.\u003c/p\u003e\n"],["\u003cp\u003eBoth the BigQuery and Drive APIs must be enabled for your project before running the sample codes, and the code samples reference the BigQuery quickstart and API reference documentation for additional assistance.\u003c/p\u003e\n"],["\u003cp\u003eThe code will initialize a client object to send requests or to interact with the BigQuery API.\u003c/p\u003e\n"],["\u003cp\u003eFor gcloud application default credentials, the user must authorize the application before, using a specific gcloud auth command.\u003c/p\u003e\n"]]],[],null,["# Create credentials with scopes\n\nCreate credentials with Drive and BigQuery API scopes.\n\nCode sample\n-----------\n\n### Java\n\n\nBefore trying this sample, follow the Java setup instructions in the\n[BigQuery quickstart using\nclient libraries](/bigquery/docs/quickstarts/quickstart-client-libraries).\n\n\nFor more information, see the\n[BigQuery Java API\nreference documentation](/java/docs/reference/google-cloud-bigquery/latest/overview).\n\n\nTo authenticate to BigQuery, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/bigquery/docs/authentication#client-libs).\n\n GoogleCredentials credentials =\n ServiceAccountCredentials.getApplicationDefault()\n .createScoped(\n ImmutableSet.of(\n \"https://www.googleapis.com/auth/bigquery\",\n \"https://www.googleapis.com/auth/drive\"));\n\n // Initialize client that will be used to send requests. This client only needs to be created\n // once, and can be reused for multiple requests.\n BigQuery bigquery =\n BigQueryOptions.newBuilder().setCredentials(credentials).build().getService();\n\n### Python\n\n\nBefore trying this sample, follow the Python setup instructions in the\n[BigQuery quickstart using\nclient libraries](/bigquery/docs/quickstarts/quickstart-client-libraries).\n\n\nFor more information, see the\n[BigQuery Python API\nreference documentation](/python/docs/reference/bigquery/latest).\n\n\nTo authenticate to BigQuery, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/bigquery/docs/authentication#client-libs).\n\n from google.cloud import https://cloud.google.com/python/docs/reference/bigquery/latest/\n import google.auth\n\n # Create credentials with Drive & BigQuery API scopes.\n # Both APIs must be enabled for your project before running this code.\n #\n # If you are using credentials from gcloud, you must authorize the\n # application first with the following command:\n #\n # gcloud auth application-default login \\\n # --scopes=https://www.googleapis.com/auth/drive,https://www.googleapis.com/auth/cloud-platform\n credentials, project = google.auth.default(\n scopes=[\n \"https://www.googleapis.com/auth/drive\",\n \"https://www.googleapis.com/auth/cloud-platform\",\n ]\n )\n\n # Construct a BigQuery client object.\n client = https://cloud.google.com/python/docs/reference/bigquery/latest/.https://cloud.google.com/python/docs/reference/bigquery/latest/google.cloud.bigquery.client.Client.html(credentials=credentials, project=project)\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=bigquery)."]]