2015-08-27 03:42:02 +03:00
|
|
|
// Copyright 2014 The Go Authors. All rights reserved.
|
2014-11-07 03:36:41 +03:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2014-06-25 01:28:46 +04:00
|
|
|
package oauth2_test
|
2014-06-24 23:44:20 +04:00
|
|
|
|
|
|
|
import (
|
2016-08-25 01:40:36 +03:00
|
|
|
"context"
|
2014-06-24 23:44:20 +04:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2017-03-02 23:04:53 +03:00
|
|
|
"net/http"
|
|
|
|
"time"
|
2014-06-25 01:28:46 +04:00
|
|
|
|
2014-11-26 22:44:45 +03:00
|
|
|
"golang.org/x/oauth2"
|
2014-06-24 23:44:20 +04:00
|
|
|
)
|
|
|
|
|
2014-12-11 10:30:13 +03:00
|
|
|
func ExampleConfig() {
|
2016-08-25 01:40:36 +03:00
|
|
|
ctx := context.Background()
|
2014-12-11 10:30:13 +03:00
|
|
|
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",
|
|
|
|
},
|
2014-06-24 23:44:20 +04:00
|
|
|
}
|
|
|
|
|
2023-09-07 20:23:22 +03:00
|
|
|
// use PKCE to protect against CSRF attacks
|
|
|
|
// https://www.ietf.org/archive/id/draft-ietf-oauth-security-topics-22.html#name-countermeasures-6
|
|
|
|
verifier := oauth2.GenerateVerifier()
|
|
|
|
|
2014-06-24 23:44:20 +04:00
|
|
|
// Redirect user to consent page to ask for permission
|
|
|
|
// for the scopes specified above.
|
2023-09-07 20:23:22 +03:00
|
|
|
url := conf.AuthCodeURL("state", oauth2.AccessTypeOffline, oauth2.S256ChallengeOption(verifier))
|
2014-06-24 23:44:20 +04:00
|
|
|
fmt.Printf("Visit the URL for the auth dialog: %v", url)
|
|
|
|
|
2016-07-21 20:18:24 +03:00
|
|
|
// 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.
|
2014-11-07 03:36:41 +03:00
|
|
|
var code string
|
2014-12-11 10:30:13 +03:00
|
|
|
if _, err := fmt.Scan(&code); err != nil {
|
2014-06-25 00:26:45 +04:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2023-09-07 20:23:22 +03:00
|
|
|
tok, err := conf.Exchange(ctx, code, oauth2.VerifierOption(verifier))
|
2014-06-24 23:44:20 +04:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-08-25 01:40:36 +03:00
|
|
|
client := conf.Client(ctx, tok)
|
2014-06-25 00:10:10 +04:00
|
|
|
client.Get("...")
|
2014-06-24 23:44:20 +04:00
|
|
|
}
|
2017-03-02 23:04:53 +03:00
|
|
|
|
2017-09-01 20:40:05 +03:00
|
|
|
func ExampleConfig_customHTTP() {
|
|
|
|
ctx := context.Background()
|
2017-03-02 23:04:53 +03:00
|
|
|
|
|
|
|
conf := &oauth2.Config{
|
|
|
|
ClientID: "YOUR_CLIENT_ID",
|
|
|
|
ClientSecret: "YOUR_CLIENT_SECRET",
|
|
|
|
Scopes: []string{"SCOPE1", "SCOPE2"},
|
|
|
|
Endpoint: oauth2.Endpoint{
|
|
|
|
TokenURL: "https://provider.com/o/oauth2/token",
|
2017-02-08 04:56:20 +03:00
|
|
|
AuthURL: "https://provider.com/o/oauth2/auth",
|
2017-03-02 23:04:53 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-09-01 20:40:05 +03:00
|
|
|
// 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 {
|
2017-02-08 04:56:20 +03:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-09-01 20:40:05 +03:00
|
|
|
// Use the custom HTTP client when requesting a token.
|
|
|
|
httpClient := &http.Client{Timeout: 2 * time.Second}
|
|
|
|
ctx = context.WithValue(ctx, oauth2.HTTPClient, httpClient)
|
2017-02-08 04:56:20 +03:00
|
|
|
|
2017-09-01 20:40:05 +03:00
|
|
|
tok, err := conf.Exchange(ctx, code)
|
2017-03-02 23:04:53 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2017-02-08 04:56:20 +03:00
|
|
|
|
2017-09-01 20:40:05 +03:00
|
|
|
client := conf.Client(ctx, tok)
|
|
|
|
_ = client
|
2017-03-02 23:04:53 +03:00
|
|
|
}
|