Source File
oauth2.go
Belonging Package
golang.org/x/oauth2
package oauth2 // import "golang.org/x/oauth2"
import (
)
func ( string) {}
AuthStyleInHeader AuthStyle = 2
)
AccessTypeOnline AuthCodeOption = SetAuthURLParam("access_type", "online")
AccessTypeOffline AuthCodeOption = SetAuthURLParam("access_type", "offline")
ApprovalForce AuthCodeOption = SetAuthURLParam("prompt", "consent")
)
func (, string) AuthCodeOption {
return setParam{, }
}
func ( *Config) ( context.Context, string, ...AuthCodeOption) (*Token, error) {
:= url.Values{
"grant_type": {"authorization_code"},
"code": {},
}
if .RedirectURL != "" {
.Set("redirect_uri", .RedirectURL)
}
for , := range {
.setValue()
}
return retrieveToken(, , )
}
func ( *Config) ( context.Context, *Token) TokenSource {
:= &tokenRefresher{
ctx: ,
conf: ,
}
if != nil {
.refreshToken = .RefreshToken
}
return &reuseTokenSource{
t: ,
new: ,
}
}
type tokenRefresher struct {
ctx context.Context // used to get HTTP requests
conf *Config
refreshToken string
}
func ( *tokenRefresher) () (*Token, error) {
if .refreshToken == "" {
return nil, errors.New("oauth2: token expired and refresh token is not set")
}
, := retrieveToken(.ctx, .conf, url.Values{
"grant_type": {"refresh_token"},
"refresh_token": {.refreshToken},
})
if != nil {
return nil,
}
if .refreshToken != .RefreshToken {
.refreshToken = .RefreshToken
}
return ,
}
type reuseTokenSource struct {
new TokenSource // called when t is expired.
mu sync.Mutex // guards t
t *Token
}
func ( *Token) TokenSource {
return staticTokenSource{}
}
type staticTokenSource struct {
t *Token
}
func ( staticTokenSource) () (*Token, error) {
return .t, nil
}
func ( context.Context, TokenSource) *http.Client {
if == nil {
return internal.ContextClient()
}
return &http.Client{
Transport: &Transport{
Base: internal.ContextClient().Transport,
Source: ReuseTokenSource(nil, ),
},
}
}
if , := .(*reuseTokenSource); {
return
}
= .new
}
return &reuseTokenSource{
t: ,
new: ,
}
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |