You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

53 lines
1.6 KiB

  1. // Copyright 2019 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. package transport_test
  15. import (
  16. "context"
  17. "log"
  18. "google.golang.org/api/option"
  19. "google.golang.org/api/transport"
  20. )
  21. func Example_applicationDefaultCredentials() {
  22. ctx := context.Background()
  23. // Providing no auth option will cause NewClient to look for Application
  24. // Default Creds as specified at https://godoc.org/golang.org/x/oauth2/google#FindDefaultCredentials.
  25. //
  26. // Note: Given the same set of options, transport.NewHTTPClient and
  27. // transport.NewGRPCClient use the same credentials.
  28. c, _, err := transport.NewHTTPClient(ctx)
  29. if err != nil {
  30. log.Fatal(err)
  31. }
  32. _ = c // Use authenticated client.
  33. }
  34. func Example_withCredentialsFile() {
  35. ctx := context.Background()
  36. // Download service account creds per https://cloud.google.com/docs/authentication/end-user.
  37. //
  38. // Note: Given the same set of options, transport.NewHTTPClient and
  39. // transport.NewGRPCClient use the same credentials.
  40. c, _, err := transport.NewHTTPClient(ctx, option.WithCredentialsFile("/path/to/service-account-creds.json"))
  41. if err != nil {
  42. log.Fatal(err)
  43. }
  44. _ = c // Use authenticated client.
  45. }