...

Source file src/cloud.google.com/go/auth/credentials/example_test.go

Documentation: cloud.google.com/go/auth/credentials

     1  // Copyright 2023 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package credentials_test
    16  
    17  import (
    18  	"log"
    19  	"os"
    20  
    21  	"cloud.google.com/go/auth/credentials"
    22  	"cloud.google.com/go/auth/httptransport"
    23  )
    24  
    25  func ExampleDetectDefault() {
    26  	creds, err := credentials.DetectDefault(&credentials.DetectOptions{
    27  		Scopes: []string{"https://www.googleapis.com/auth/devstorage.full_control"},
    28  	})
    29  	if err != nil {
    30  		log.Fatal(err)
    31  	}
    32  	client, err := httptransport.NewClient(&httptransport.Options{
    33  		Credentials: creds,
    34  	})
    35  	if err != nil {
    36  		log.Fatal(err)
    37  	}
    38  	client.Get("...")
    39  }
    40  
    41  func ExampleDetectDefault_withFilepath() {
    42  	// Your credentials should be obtained from the Google
    43  	// Developer Console (https://console.developers.google.com).
    44  	// Navigate to your project, then see the "Credentials" page
    45  	// under "APIs & Auth".
    46  	// To create a service account client, click "Create new Client ID",
    47  	// select "Service Account", and click "Create Client ID". A JSON
    48  	// key file will then be downloaded to your computer.
    49  	filepath := "/path/to/your-project-key.json"
    50  	creds, err := credentials.DetectDefault(&credentials.DetectOptions{
    51  		Scopes:          []string{"https://www.googleapis.com/auth/bigquery"},
    52  		CredentialsFile: filepath,
    53  	})
    54  	if err != nil {
    55  		log.Fatal(err)
    56  	}
    57  	client, err := httptransport.NewClient(&httptransport.Options{
    58  		Credentials: creds,
    59  	})
    60  	if err != nil {
    61  		log.Fatal(err)
    62  	}
    63  	client.Get("...")
    64  }
    65  
    66  func ExampleDetectDefault_withJSON() {
    67  	data, err := os.ReadFile("/path/to/key-file.json")
    68  	if err != nil {
    69  		log.Fatal(err)
    70  	}
    71  	creds, err := credentials.DetectDefault(&credentials.DetectOptions{
    72  		Scopes:          []string{"https://www.googleapis.com/auth/bigquery"},
    73  		CredentialsJSON: data,
    74  	})
    75  	if err != nil {
    76  		log.Fatal(err)
    77  	}
    78  	client, err := httptransport.NewClient(&httptransport.Options{
    79  		Credentials: creds,
    80  	})
    81  	if err != nil {
    82  		log.Fatal(err)
    83  	}
    84  	client.Get("...")
    85  }
    86  

View as plain text