example_test.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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 oauth2_test
  5. import (
  6. "fmt"
  7. "log"
  8. "golang.org/x/oauth2"
  9. )
  10. func ExampleConfig() {
  11. conf := &oauth2.Config{
  12. ClientID: "YOUR_CLIENT_ID",
  13. ClientSecret: "YOUR_CLIENT_SECRET",
  14. Scopes: []string{"SCOPE1", "SCOPE2"},
  15. Endpoint: oauth2.Endpoint{
  16. AuthURL: "https://provider.com/o/oauth2/auth",
  17. TokenURL: "https://provider.com/o/oauth2/token",
  18. },
  19. }
  20. // Redirect user to consent page to ask for permission
  21. // for the scopes specified above.
  22. url := conf.AuthCodeURL("state", oauth2.AccessTypeOffline)
  23. fmt.Printf("Visit the URL for the auth dialog: %v", url)
  24. // Use the authorization code that is pushed to the redirect URL.
  25. // NewTransportWithCode will do the handshake to retrieve
  26. // an access token and initiate a Transport that is
  27. // authorized and authenticated by the retrieved token.
  28. var code string
  29. if _, err := fmt.Scan(&code); err != nil {
  30. log.Fatal(err)
  31. }
  32. tok, err := conf.Exchange(oauth2.NoContext, code)
  33. if err != nil {
  34. log.Fatal(err)
  35. }
  36. client := conf.Client(oauth2.NoContext, tok)
  37. client.Get("...")
  38. }