文档:https://pkg.go.dev/golang.org/x/oauth2
oauth2包包含了OAuth 2.0规范的客户端实现

type TokenSource

  1. type TokenSource interface {
  2. Token() (*Token, error)
  3. }
  4. func ReuseTokenSource(t *Token, src TokenSource) TokenSource
  5. func StaticTokenSource(t *Token) TokenSource

type Token

令牌表示用于授权访问OAuth 2.0提供者后端的受保护资源的请求的凭据。 这个包的大多数用户不应该直接访问Token字段。它们主要被导出,供实现派生OAuth2流的相关包使用

  1. type Token struct {
  2. AccessToken string `json:"access_token"`
  3. TokenType string `json:"token_type,omitempty"`
  4. RefreshToken string `json:"refresh_token,omitempty"`
  5. Expiry time.Time `json:"expiry,omitempty"`
  6. }
  7. // Extra返回一个额外的字段。额外的字段是服务器作为令牌检索响应的一部分返回
  8. func (t *Token) Extra(key string) interface{}
  9. func (t *Token) SetAuthHeader(r *http.Request) // sets the Authorization header to r
  10. func (t *Token) Type() string
  11. func (t *Token) Valid() bool

type AuthCodeOption

An AuthCodeOption is passed to Config.AuthCodeURL

  1. type AuthCodeOption interface {
  2. // contains filtered or unexported methods
  3. }
  4. var (
  5. AccessTypeOnline AuthCodeOption = SetAuthURLParam("access_type", "online")
  6. AccessTypeOffline AuthCodeOption = SetAuthURLParam("access_type", "offline")
  7. // ApprovalForce forces the users to view the consent dialog
  8. // and confirm the permissions request at the URL returned
  9. // from AuthCodeURL, even if they've already done so.
  10. ApprovalForce AuthCodeOption = SetAuthURLParam("prompt", "consent")
  11. )
  12. func SetAuthURLParam(key, value string) AuthCodeOption

type AuthStyle

AuthStyle表示如何向服务器验证令牌请求

  1. const (
  2. // 自动检测
  3. AuthStyleAutoDetect AuthStyle = 0
  4. // AuthStyleInParams sends the "client_id" and "client_secret"in the POST
  5. AuthStyleInParams AuthStyle = 1
  6. // AuthStyleInHeader sends the client_id and client_password
  7. // using HTTP Basic Authorization
  8. AuthStyleInHeader AuthStyle = 2
  9. )

type Config

  1. type Config struct {
  2. // ClientID is the application's ID.
  3. ClientID string
  4. // ClientSecret is the application's secret.
  5. ClientSecret string
  6. // Endpoint contains the resource server's token endpoint URLs
  7. Endpoint Endpoint
  8. // RedirectURL is the URL to redirect users going through
  9. // the OAuth flow, after the resource owner's URLs.
  10. RedirectURL string
  11. // Scope specifies optional requested permissions.
  12. Scopes []string
  13. }
  14. // AuthCodeURL返回一个URL到OAuth 2.0提供商的许可页面
  15. func (c *Config) AuthCodeURL(state string, opts ...AuthCodeOption) string
  16. // 使用提供的令牌返回一个HTTP客户端。令牌将根据需要自动刷新。
  17. // 底层的HTTP传输将使用提供的上下文获得
  18. func (c *Config) Client(ctx context.Context, t *Token) *http.Client
  19. // Exchange将授权代码(code)转换为令牌
  20. func (c *Config) Exchange(ctx context.Context, code string, opts ...AuthCodeOption) (*Token, error)
  21. func (c *Config) TokenSource(ctx context.Context, t *Token) TokenSource
  22. func (c *Config) PasswordCredentialsToken(ctx context.Context, username, password string) (*Token, error)
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "log"
  6. "golang.org/x/oauth2"
  7. )
  8. func main() {
  9. ctx := context.Background()
  10. conf := &oauth2.Config{
  11. ClientID: "YOUR_CLIENT_ID",
  12. ClientSecret: "YOUR_CLIENT_SECRET",
  13. Scopes: []string{"SCOPE1", "SCOPE2"},
  14. Endpoint: oauth2.Endpoint{
  15. AuthURL: "https://provider.com/o/oauth2/auth",
  16. TokenURL: "https://provider.com/o/oauth2/token",
  17. },
  18. }
  19. // Redirect user to consent page to ask for permission
  20. // for the scopes specified above.
  21. url := conf.AuthCodeURL("state", oauth2.AccessTypeOffline)
  22. fmt.Printf("Visit the URL for the auth dialog: %v\n", url)
  23. // Use the authorization code that is pushed to the redirect
  24. // URL. Exchange will do the handshake to retrieve the
  25. // initial access token. The HTTP Client returned by
  26. // conf.Client will refresh the token as necessary.
  27. var code string
  28. if _, err := fmt.Scan(&code); err != nil {
  29. log.Fatal(err)
  30. }
  31. tok, err := conf.Exchange(ctx, code)
  32. if err != nil {
  33. log.Fatal(err)
  34. }
  35. client := conf.Client(ctx, tok)
  36. client.Get("...")
  37. }

type Endpoint

Endpoint 表示OAuth 2.0提供商的授权和令牌端点url

  1. type Endpoint struct {
  2. AuthURL string
  3. TokenURL string
  4. // AuthStyle optionally specifies how the endpoint wants the
  5. // client ID & client secret sent. The zero value means to
  6. // auto-detect.
  7. AuthStyle AuthStyle
  8. }