身份验证使用入门

应用默认凭据 (ADC) 可让您的应用使用服务账号凭据作为自身身份来访问 BigQuery 资源。

准备工作

Select the tab for how you plan to use the samples on this page:

C#

如需在本地开发环境中使用本页面上的 .NET 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭据设置应用默认凭据。

  1. Install the Google Cloud CLI.
  2. To initialize the gcloud CLI, run the following command:

    gcloud init
  3. If you're using a local shell, then create local authentication credentials for your user account:

    gcloud auth application-default login

    You don't need to do this if you're using Cloud Shell.

如需了解详情,请参阅 Set up authentication for a local development environment。 如需了解详情,请参阅身份验证文档中的为本地开发环境设置 ADC

Go

如需在本地开发环境中使用本页面上的 Go 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭据设置应用默认凭据。

  1. Install the Google Cloud CLI.
  2. To initialize the gcloud CLI, run the following command:

    gcloud init
  3. If you're using a local shell, then create local authentication credentials for your user account:

    gcloud auth application-default login

    You don't need to do this if you're using Cloud Shell.

如需了解详情,请参阅 Set up authentication for a local development environment。 如需了解详情,请参阅身份验证文档中的为本地开发环境设置 ADC

Java

如需在本地开发环境中使用本页面上的 Java 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭据设置应用默认凭据。

  1. Install the Google Cloud CLI.
  2. To initialize the gcloud CLI, run the following command:

    gcloud init
  3. If you're using a local shell, then create local authentication credentials for your user account:

    gcloud auth application-default login

    You don't need to do this if you're using Cloud Shell.

如需了解详情,请参阅 Set up authentication for a local development environment。 如需了解详情,请参阅身份验证文档中的为本地开发环境设置 ADC

Node.js

如需在本地开发环境中使用本页面上的 Node.js 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭据设置应用默认凭据。

  1. Install the Google Cloud CLI.
  2. To initialize the gcloud CLI, run the following command:

    gcloud init
  3. If you're using a local shell, then create local authentication credentials for your user account:

    gcloud auth application-default login

    You don't need to do this if you're using Cloud Shell.

如需了解详情,请参阅 Set up authentication for a local development environment。 如需了解详情,请参阅身份验证文档中的为本地开发环境设置 ADC

PHP

如需在本地开发环境中使用本页面上的 PHP 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭据设置应用默认凭据。

  1. Install the Google Cloud CLI.
  2. To initialize the gcloud CLI, run the following command:

    gcloud init
  3. If you're using a local shell, then create local authentication credentials for your user account:

    gcloud auth application-default login

    You don't need to do this if you're using Cloud Shell.

如需了解详情,请参阅 Set up authentication for a local development environment

Python

如需在本地开发环境中使用本页面上的 Python 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭据设置应用默认凭据。

  1. Install the Google Cloud CLI.
  2. To initialize the gcloud CLI, run the following command:

    gcloud init
  3. If you're using a local shell, then create local authentication credentials for your user account:

    gcloud auth application-default login

    You don't need to do this if you're using Cloud Shell.

如需了解详情,请参阅 Set up authentication for a local development environment。 如需了解详情,请参阅身份验证文档中的为本地开发环境设置 ADC

Ruby

如需在本地开发环境中使用本页面上的 Ruby 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭据设置应用默认凭据。

  1. Install the Google Cloud CLI.
  2. To initialize the gcloud CLI, run the following command:

    gcloud init
  3. If you're using a local shell, then create local authentication credentials for your user account:

    gcloud auth application-default login

    You don't need to do this if you're using Cloud Shell.

如需了解详情,请参阅 Set up authentication for a local development environment。 如需了解详情,请参阅身份验证文档中的为本地开发环境设置 ADC

如需了解如何为生产环境设置身份验证,请参阅 Set up Application Default Credentials for code running on Google Cloud

应用默认凭据

客户端库可以使用应用默认凭据轻松进行 Google API 身份验证,并向这些 API 发送请求。借助应用默认凭据,您可以在本地测试应用并部署它,无需更改底层代码。如需了解详情,请参阅使用客户端库时进行身份验证

如果您使用 BigQuery 客户端库创建服务对象且不传入显式凭据,则您的应用会使用应用默认凭据进行身份验证。 以下示例展示了如何使用 ADC 对 BigQuery 进行身份验证。

C#

试用此示例之前,请按照 BigQuery 快速入门:使用客户端库中的 C# 设置说明进行操作。 如需了解详情,请参阅 BigQuery C# API 参考文档

如需向 BigQuery 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅准备工作

 using Google.Apis.Bigquery.v2.Data; using Google.Cloud.BigQuery.V2;  public class BigQueryCreateDataset {     public BigQueryDataset CreateDataset(         string projectId = "your-project-id",         string location = "US"     )     {         BigQueryClient client = BigQueryClient.Create(projectId);         var dataset = new Dataset         {             // Specify the geographic location where the dataset should reside.             Location = location         };         // Create the dataset         return client.CreateDataset(             datasetId: "your_new_dataset_id", dataset);     } }

Go

试用此示例之前,请按照 BigQuery 快速入门:使用客户端库中的 Go 设置说明进行操作。 如需了解详情,请参阅 BigQuery Go API 参考文档

如需向 BigQuery 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅准备工作

 // Sample bigquery-quickstart creates a Google BigQuery dataset. package main  import ( 	"context" 	"fmt" 	"log"  	"cloud.google.com/go/bigquery" )  func main() { 	ctx := context.Background()  	// Sets your Google Cloud Platform project ID. 	projectID := "YOUR_PROJECT_ID"  	// Creates a client. 	client, err := bigquery.NewClient(ctx, projectID) 	if err != nil { 		log.Fatalf("bigquery.NewClient: %v", err) 	} 	defer client.Close()  	// Sets the name for the new dataset. 	datasetName := "my_new_dataset"  	// Creates the new BigQuery dataset. 	if err := client.Dataset(datasetName).Create(ctx, &bigquery.DatasetMetadata{}); err != nil { 		log.Fatalf("Failed to create dataset: %v", err) 	}  	fmt.Printf("Dataset created\n") } 

Java

试用此示例之前,请按照 BigQuery 快速入门:使用客户端库中的 Java 设置说明进行操作。 如需了解详情,请参阅 BigQuery Java API 参考文档

如需向 BigQuery 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅准备工作

public static void implicit() {   // Instantiate a client. If you don't specify credentials when constructing a client, the   // client library will look for credentials in the environment, such as the   // GOOGLE_APPLICATION_CREDENTIALS environment variable.   BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();    // Use the client.   System.out.println("Datasets:");   for (Dataset dataset : bigquery.listDatasets().iterateAll()) {     System.out.printf("%s%n", dataset.getDatasetId().getDataset());   } }

Node.js

试用此示例之前,请按照 BigQuery 快速入门:使用客户端库中的 Node.js 设置说明进行操作。 如需了解详情,请参阅 BigQuery Node.js API 参考文档

如需向 BigQuery 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅准备工作

// Import the Google Cloud client library using default credentials const {BigQuery} = require('@google-cloud/bigquery'); const bigquery = new BigQuery();

PHP

试用此示例之前,请按照 BigQuery 快速入门:使用客户端库中的 PHP 设置说明进行操作。 如需了解详情,请参阅 BigQuery PHP API 参考文档

如需向 BigQuery 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅准备工作

use Google\Cloud\BigQuery\BigQueryClient;  /** Uncomment and populate these variables in your code */ //$projectId = 'The Google project ID';  $bigQuery = new BigQueryClient([     'projectId' => $projectId, ]);

Python

试用此示例之前,请按照 BigQuery 快速入门:使用客户端库中的 Python 设置说明进行操作。 如需了解详情,请参阅 BigQuery Python API 参考文档

如需向 BigQuery 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅准备工作

from google.cloud import bigquery  # If you don't specify credentials when constructing the client, the # client library will look for credentials in the environment. client = bigquery.Client()

Ruby

试用此示例之前,请按照 BigQuery 快速入门:使用客户端库中的 Ruby 设置说明进行操作。 如需了解详情,请参阅 BigQuery Ruby API 参考文档

如需向 BigQuery 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅准备工作

require "google/cloud/bigquery"  # This uses Application Default Credentials to authenticate. # @see https://cloud.google.com/bigquery/docs/authentication/getting-started bigquery = Google::Cloud::Bigquery.new  sql     = "SELECT " \           "CONCAT('https://stackoverflow.com/questions/', CAST(id as STRING)) as url, view_count " \           "FROM `bigquery-public-data.stackoverflow.posts_questions` " \           "WHERE tags like '%google-bigquery%' " \           "ORDER BY view_count DESC LIMIT 10" results = bigquery.query sql  results.each do |row|   puts "#{row[:url]}: #{row[:view_count]} views" end

后续步骤