oauth2util.go 886 B

12345678910111213141516171819202122232425262728293031323334
  1. package oauth2util
  2. import (
  3. "fmt"
  4. "log"
  5. "golang.org/x/oauth2"
  6. )
  7. //GetTokenFromWeb shows link to user, and requests the informed token
  8. func GetTokenFromWeb(config *oauth2.Config) *oauth2.Token {
  9. //authURL := config.AuthCodeURL("state-token", oauth2.AccessTypeOffline)
  10. authURL := config.AuthCodeURL("state-token")
  11. fmt.Printf(
  12. `Go to the following link in your browser:
  13. ----------------------------------------------------------------------------------------------
  14. %v
  15. ----------------------------------------------------------------------------------------------
  16. type the authorization code: `, authURL)
  17. var code string
  18. if _, err := fmt.Scan(&code); err != nil {
  19. log.Fatalf("Unable to read authorization code %v", err)
  20. }
  21. tok, err := config.Exchange(oauth2.NoContext, code)
  22. if err != nil {
  23. log.Fatalf("Unable to retrieve token from web: %v", err)
  24. }
  25. return tok
  26. }