多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
[TOC] ## 概述 符合 oauth2 的 请求 ## 常用 oath2 | Path | Synopsis | | --- | --- | | [amazon](https://pkg.go.dev/golang.org/x/oauth2@v0.0.0-20201208152858-08078c50e5b5/amazon) | Package amazon provides constants for using OAuth2 to access Amazon. | | [clientcredentials](https://pkg.go.dev/golang.org/x/oauth2@v0.0.0-20201208152858-08078c50e5b5/clientcredentials) | Package clientcredentials implements the OAuth2.0 "client credentials" token flow, also known as the "two-legged OAuth 2.0". | | [endpoints](https://pkg.go.dev/golang.org/x/oauth2@v0.0.0-20201208152858-08078c50e5b5/endpoints) | Package endpoints provides constants for using OAuth2 to access various services. | | [facebook](https://pkg.go.dev/golang.org/x/oauth2@v0.0.0-20201208152858-08078c50e5b5/facebook) | Package facebook provides constants for using OAuth2 to access Facebook. | | [github](https://pkg.go.dev/golang.org/x/oauth2@v0.0.0-20201208152858-08078c50e5b5/github) | Package github provides constants for using OAuth2 to access Github. | | [gitlab](https://pkg.go.dev/golang.org/x/oauth2@v0.0.0-20201208152858-08078c50e5b5/gitlab) | Package gitlab provides constants for using OAuth2 to access GitLab. | | [google](https://pkg.go.dev/golang.org/x/oauth2@v0.0.0-20201208152858-08078c50e5b5/google) | Package google provides support for making OAuth2 authorized and authenticated HTTP requests to Google APIs. | | [instagram](https://pkg.go.dev/golang.org/x/oauth2@v0.0.0-20201208152858-08078c50e5b5/instagram) | Package instagram provides constants for using OAuth2 to access Instagram. | | [internal](https://pkg.go.dev/golang.org/x/oauth2@v0.0.0-20201208152858-08078c50e5b5/internal) | Package internal contains support packages for oauth2 package. | | [jira](https://pkg.go.dev/golang.org/x/oauth2@v0.0.0-20201208152858-08078c50e5b5/jira) | Package jira provides claims and JWT signing for OAuth2 to access JIRA/Confluence. | | [jws](https://pkg.go.dev/golang.org/x/oauth2@v0.0.0-20201208152858-08078c50e5b5/jws) | Package jws provides a partial implementation of JSON Web Signature encoding and decoding. | | [jwt](https://pkg.go.dev/golang.org/x/oauth2@v0.0.0-20201208152858-08078c50e5b5/jwt) | Package jwt implements the OAuth 2.0 JSON Web Token flow, commonly known as "two-legged OAuth 2.0". | | [linkedin](https://pkg.go.dev/golang.org/x/oauth2@v0.0.0-20201208152858-08078c50e5b5/linkedin) | Package linkedin provides constants for using OAuth2 to access LinkedIn. | | [mailchimp](https://pkg.go.dev/golang.org/x/oauth2@v0.0.0-20201208152858-08078c50e5b5/mailchimp) | Package mailchimp provides constants for using OAuth2 to access MailChimp. | | [mailru](https://pkg.go.dev/golang.org/x/oauth2@v0.0.0-20201208152858-08078c50e5b5/mailru) | Package mailru provides constants for using OAuth2 to access Mail.Ru. | | [mediamath](https://pkg.go.dev/golang.org/x/oauth2@v0.0.0-20201208152858-08078c50e5b5/mediamath) | Package mediamath provides constants for using OAuth2 to access MediaMath. | | [microsoft](https://pkg.go.dev/golang.org/x/oauth2@v0.0.0-20201208152858-08078c50e5b5/microsoft) | Package microsoft provides constants for using OAuth2 to access Windows Live ID. | | [nokiahealth](https://pkg.go.dev/golang.org/x/oauth2@v0.0.0-20201208152858-08078c50e5b5/nokiahealth) | Package nokiahealth provides constants for using OAuth2 to access the Nokia Health Mate API. | | [odnoklassniki](https://pkg.go.dev/golang.org/x/oauth2@v0.0.0-20201208152858-08078c50e5b5/odnoklassniki) | Package odnoklassniki provides constants for using OAuth2 to access Odnoklassniki. | | [paypal](https://pkg.go.dev/golang.org/x/oauth2@v0.0.0-20201208152858-08078c50e5b5/paypal) | Package paypal provides constants for using OAuth2 to access PayPal. | | [slack](https://pkg.go.dev/golang.org/x/oauth2@v0.0.0-20201208152858-08078c50e5b5/slack) | Package slack provides constants for using OAuth2 to access Slack. | | [spotify](https://pkg.go.dev/golang.org/x/oauth2@v0.0.0-20201208152858-08078c50e5b5/spotify) | Package spotify provides constants for using OAuth2 to access Spotify. | | [stackoverflow](https://pkg.go.dev/golang.org/x/oauth2@v0.0.0-20201208152858-08078c50e5b5/stackoverflow) | Package stackoverflow provides constants for using OAuth2 to access Stack Overflow. | | [twitch](https://pkg.go.dev/golang.org/x/oauth2@v0.0.0-20201208152858-08078c50e5b5/twitch) | Package twitch provides constants for using OAuth2 to access Twitch. | | [uber](https://pkg.go.dev/golang.org/x/oauth2@v0.0.0-20201208152858-08078c50e5b5/uber) | Package uber provides constants for using OAuth2 to access Uber. | | [vk](https://pkg.go.dev/golang.org/x/oauth2@v0.0.0-20201208152858-08078c50e5b5/vk) | Package vk provides constants for using OAuth2 to access VK.com. | | [yahoo](https://pkg.go.dev/golang.org/x/oauth2@v0.0.0-20201208152858-08078c50e5b5/yahoo) | Package yahoo provides constants for using OAuth2 to access Yahoo. | | [yandex](https://pkg.go.dev/golang.org/x/oauth2@v0.0.0-20201208152858-08078c50e5b5/yandex) | Package yandex provides constants for using OAuth2 to access Yandex APIs. | ## 语法 ### Func ``` func NewClient(ctx context.Context, src TokenSource) *http.Client func RegisterBrokenAuthHeaderProvider(tokenURL string) ``` ### Type ``` type AuthCodeOption func SetAuthURLParam(key, value string) AuthCodeOption type AuthStyle type Config func (c *Config) AuthCodeURL(state string, opts ...AuthCodeOption) string func (c *Config) Client(ctx context.Context, t *Token) *http.Client func (c *Config) Exchange(ctx context.Context, code string, opts ...AuthCodeOption) (*Token, error) func (c *Config) PasswordCredentialsToken(ctx context.Context, username, password string) (*Token, error) func (c *Config) TokenSource(ctx context.Context, t *Token) TokenSource type Endpoint type RetrieveError func (r *RetrieveError) Error() string type Token func (t *Token) Extra(key string) interface{} func (t *Token) SetAuthHeader(r *http.Request) func (t *Token) Type() string func (t *Token) Valid() bool func (t *Token) WithExtra(extra interface{}) *Token type TokenSource func ReuseTokenSource(t *Token, src TokenSource) TokenSource func StaticTokenSource(t *Token) TokenSource type Transport func (t *Transport) CancelRequest(req *http.Request) func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) ``` ## 实例 ### CustomHTTP ``` package main import ( "context" "fmt" "golang.org/x/oauth2" "log" ) func main() { ctx := context.Background() conf := &oauth2.Config{ ClientID: "YOUR_CLIENT_ID", ClientSecret: "YOUR_CLIENT_SECRET", Scopes: []string{"SCOPE1", "SCOPE2"}, Endpoint: oauth2.Endpoint{ AuthURL: "https://provider.com/o/oauth2/auth", TokenURL: "https://provider.com/o/oauth2/token", }, } // Redirect user to consent page to ask for permission // for the scopes specified above. url := conf.AuthCodeURL("state", oauth2.AccessTypeOffline) fmt.Printf("Visit the URL for the auth dialog: %v", url) // Use the authorization code that is pushed to the redirect // URL. Exchange will do the handshake to retrieve the // initial access token. The HTTP Client returned by // conf.Client will refresh the token as necessary. var code string if _, err := fmt.Scan(&code); err != nil { log.Fatal(err) } tok, err := conf.Exchange(ctx, code) if err != nil { log.Fatal(err) } client := conf.Client(ctx, tok) client.Get("...") } ```