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.
 
 
 

147 rivejä
4.5 KiB

  1. // Copyright 2014 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package google_test
  5. import (
  6. "context"
  7. "fmt"
  8. "io/ioutil"
  9. "log"
  10. "net/http"
  11. "golang.org/x/oauth2"
  12. "golang.org/x/oauth2/google"
  13. "golang.org/x/oauth2/jwt"
  14. )
  15. func ExampleDefaultClient() {
  16. client, err := google.DefaultClient(oauth2.NoContext,
  17. "https://www.googleapis.com/auth/devstorage.full_control")
  18. if err != nil {
  19. log.Fatal(err)
  20. }
  21. client.Get("...")
  22. }
  23. func Example_webServer() {
  24. // Your credentials should be obtained from the Google
  25. // Developer Console (https://console.developers.google.com).
  26. conf := &oauth2.Config{
  27. ClientID: "YOUR_CLIENT_ID",
  28. ClientSecret: "YOUR_CLIENT_SECRET",
  29. RedirectURL: "YOUR_REDIRECT_URL",
  30. Scopes: []string{
  31. "https://www.googleapis.com/auth/bigquery",
  32. "https://www.googleapis.com/auth/blogger",
  33. },
  34. Endpoint: google.Endpoint,
  35. }
  36. // Redirect user to Google's consent page to ask for permission
  37. // for the scopes specified above.
  38. url := conf.AuthCodeURL("state")
  39. fmt.Printf("Visit the URL for the auth dialog: %v", url)
  40. // Handle the exchange code to initiate a transport.
  41. tok, err := conf.Exchange(oauth2.NoContext, "authorization-code")
  42. if err != nil {
  43. log.Fatal(err)
  44. }
  45. client := conf.Client(oauth2.NoContext, tok)
  46. client.Get("...")
  47. }
  48. func ExampleJWTConfigFromJSON() {
  49. // Your credentials should be obtained from the Google
  50. // Developer Console (https://console.developers.google.com).
  51. // Navigate to your project, then see the "Credentials" page
  52. // under "APIs & Auth".
  53. // To create a service account client, click "Create new Client ID",
  54. // select "Service Account", and click "Create Client ID". A JSON
  55. // key file will then be downloaded to your computer.
  56. data, err := ioutil.ReadFile("/path/to/your-project-key.json")
  57. if err != nil {
  58. log.Fatal(err)
  59. }
  60. conf, err := google.JWTConfigFromJSON(data, "https://www.googleapis.com/auth/bigquery")
  61. if err != nil {
  62. log.Fatal(err)
  63. }
  64. // Initiate an http.Client. The following GET request will be
  65. // authorized and authenticated on the behalf of
  66. // your service account.
  67. client := conf.Client(oauth2.NoContext)
  68. client.Get("...")
  69. }
  70. func ExampleSDKConfig() {
  71. // The credentials will be obtained from the first account that
  72. // has been authorized with `gcloud auth login`.
  73. conf, err := google.NewSDKConfig("")
  74. if err != nil {
  75. log.Fatal(err)
  76. }
  77. // Initiate an http.Client. The following GET request will be
  78. // authorized and authenticated on the behalf of the SDK user.
  79. client := conf.Client(oauth2.NoContext)
  80. client.Get("...")
  81. }
  82. func Example_serviceAccount() {
  83. // Your credentials should be obtained from the Google
  84. // Developer Console (https://console.developers.google.com).
  85. conf := &jwt.Config{
  86. Email: "xxx@developer.gserviceaccount.com",
  87. // The contents of your RSA private key or your PEM file
  88. // that contains a private key.
  89. // If you have a p12 file instead, you
  90. // can use `openssl` to export the private key into a pem file.
  91. //
  92. // $ openssl pkcs12 -in key.p12 -passin pass:notasecret -out key.pem -nodes
  93. //
  94. // The field only supports PEM containers with no passphrase.
  95. // The openssl command will convert p12 keys to passphrase-less PEM containers.
  96. PrivateKey: []byte("-----BEGIN RSA PRIVATE KEY-----..."),
  97. Scopes: []string{
  98. "https://www.googleapis.com/auth/bigquery",
  99. "https://www.googleapis.com/auth/blogger",
  100. },
  101. TokenURL: google.JWTTokenURL,
  102. // If you would like to impersonate a user, you can
  103. // create a transport with a subject. The following GET
  104. // request will be made on the behalf of user@example.com.
  105. // Optional.
  106. Subject: "user@example.com",
  107. }
  108. // Initiate an http.Client, the following GET request will be
  109. // authorized and authenticated on the behalf of user@example.com.
  110. client := conf.Client(oauth2.NoContext)
  111. client.Get("...")
  112. }
  113. func ExampleComputeTokenSource() {
  114. client := &http.Client{
  115. Transport: &oauth2.Transport{
  116. // Fetch from Google Compute Engine's metadata server to retrieve
  117. // an access token for the provided account.
  118. // If no account is specified, "default" is used.
  119. Source: google.ComputeTokenSource(""),
  120. },
  121. }
  122. client.Get("...")
  123. }
  124. func ExampleCredentialsFromJSON() {
  125. ctx := context.Background()
  126. data, err := ioutil.ReadFile("/path/to/key-file.json")
  127. if err != nil {
  128. log.Fatal(err)
  129. }
  130. creds, err := google.CredentialsFromJSON(ctx, data, "https://www.googleapis.com/auth/bigquery")
  131. if err != nil {
  132. log.Fatal(err)
  133. }
  134. _ = creds // TODO: Use creds.
  135. }