...

Source file src/google.golang.org/api/transport/examples_test.go

Documentation: google.golang.org/api/transport

     1  // Copyright 2019 Google LLC.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package transport_test
     6  
     7  import (
     8  	"context"
     9  	"log"
    10  
    11  	"google.golang.org/api/option"
    12  	"google.golang.org/api/transport"
    13  )
    14  
    15  func Example_applicationDefaultCredentials() {
    16  	ctx := context.Background()
    17  
    18  	// Providing no auth option will cause NewClient to look for Application
    19  	// Default Creds as specified at https://godoc.org/golang.org/x/oauth2/google#FindDefaultCredentials.
    20  	//
    21  	// Note: Given the same set of options, transport.NewHTTPClient and
    22  	// transport.DialGRPC use the same credentials.
    23  	c, _, err := transport.NewHTTPClient(ctx)
    24  	if err != nil {
    25  		log.Fatal(err)
    26  	}
    27  	_ = c // Use authenticated client.
    28  }
    29  
    30  func Example_withCredentialsFile() {
    31  	ctx := context.Background()
    32  
    33  	// Download service account creds per https://cloud.google.com/docs/authentication/production.
    34  	//
    35  	// Note: Given the same set of options, transport.NewHTTPClient and
    36  	// transport.DialGRPC use the same credentials.
    37  	c, _, err := transport.NewHTTPClient(ctx, option.WithCredentialsFile("/path/to/service-account-creds.json"))
    38  	if err != nil {
    39  		log.Fatal(err)
    40  	}
    41  	_ = c // Use authenticated client.
    42  }
    43  

View as plain text