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.
 
 
 

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