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.
 
 
 

74 lines
2.5 KiB

  1. // Copyright 2016 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 cloud_test
  15. import (
  16. "context"
  17. "cloud.google.com/go/datastore"
  18. "cloud.google.com/go/pubsub"
  19. "golang.org/x/oauth2/google"
  20. "google.golang.org/api/option"
  21. )
  22. // Google Application Default Credentials is the recommended way to authorize
  23. // and authenticate clients.
  24. //
  25. // For information on how to create and obtain Application Default Credentials, see
  26. // https://developers.google.com/identity/protocols/application-default-credentials.
  27. func Example_applicationDefaultCredentials() {
  28. client, err := datastore.NewClient(context.Background(), "project-id")
  29. if err != nil {
  30. // TODO: handle error.
  31. }
  32. _ = client // Use the client.
  33. }
  34. // You can use a file with credentials to authenticate and authorize, such as a JSON
  35. // key file associated with a Google service account. Service Account keys can be
  36. // created and downloaded from
  37. // https://console.developers.google.com/permissions/serviceaccounts.
  38. //
  39. // This example uses the Datastore client, but the same steps apply to
  40. // the other client libraries underneath this package.
  41. func Example_credentialsFile() {
  42. client, err := datastore.NewClient(context.Background(),
  43. "project-id", option.WithCredentialsFile("/path/to/service-account-key.json"))
  44. if err != nil {
  45. // TODO: handle error.
  46. }
  47. _ = client // Use the client.
  48. }
  49. // In some cases (for instance, you don't want to store secrets on disk), you can
  50. // create credentials from in-memory JSON and use the WithCredentials option.
  51. //
  52. // The google package in this example is at golang.org/x/oauth2/google.
  53. //
  54. // This example uses the PubSub client, but the same steps apply to
  55. // the other client libraries underneath this package.
  56. func Example_credentialsFromJSON() {
  57. ctx := context.Background()
  58. creds, err := google.CredentialsFromJSON(ctx, []byte("JSON creds"), pubsub.ScopePubSub)
  59. if err != nil {
  60. // TODO: handle error.
  61. }
  62. client, err := pubsub.NewClient(ctx, "project-id", option.WithCredentials(creds))
  63. if err != nil {
  64. // TODO: handle error.
  65. }
  66. _ = client // Use the client.
  67. }