2014-11-06 19:36:41 -05:00
|
|
|
// Copyright 2014 The oauth2 Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2014-06-24 17:28:46 -04:00
|
|
|
package oauth2_test
|
2014-06-24 15:44:20 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2014-06-24 17:28:46 -04:00
|
|
|
"testing"
|
|
|
|
|
2014-11-26 14:44:45 -05:00
|
|
|
"golang.org/x/oauth2"
|
2014-06-24 15:44:20 -04:00
|
|
|
)
|
|
|
|
|
2014-06-24 17:53:39 -04:00
|
|
|
// TODO(jbd): Remove after Go 1.4.
|
|
|
|
// Related to https://codereview.appspot.com/107320046
|
2014-06-24 17:28:46 -04:00
|
|
|
func TestA(t *testing.T) {}
|
|
|
|
|
2014-12-11 02:30:13 -05:00
|
|
|
func ExampleConfig() {
|
|
|
|
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 15:44:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Redirect user to consent page to ask for permission
|
|
|
|
// for the scopes specified above.
|
2014-12-11 02:30:13 -05:00
|
|
|
url := conf.AuthCodeURL("state", oauth2.AccessTypeOffline)
|
2014-06-24 15:44:20 -04:00
|
|
|
fmt.Printf("Visit the URL for the auth dialog: %v", url)
|
|
|
|
|
2014-08-16 22:29:11 -04:00
|
|
|
// Use the authorization code that is pushed to the redirect URL.
|
2014-06-24 15:44:20 -04:00
|
|
|
// NewTransportWithCode will do the handshake to retrieve
|
2014-08-16 22:29:11 -04:00
|
|
|
// an access token and initiate a Transport that is
|
|
|
|
// authorized and authenticated by the retrieved token.
|
2014-11-06 19:36:41 -05:00
|
|
|
var code string
|
2014-12-11 02:30:13 -05:00
|
|
|
if _, err := fmt.Scan(&code); err != nil {
|
2014-06-24 16:26:45 -04:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2014-12-11 02:30:13 -05:00
|
|
|
tok, err := conf.Exchange(oauth2.NoContext, code)
|
2014-06-24 15:44:20 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-12-11 02:30:13 -05:00
|
|
|
client := conf.Client(oauth2.NoContext, tok)
|
2014-06-24 16:10:10 -04:00
|
|
|
client.Get("...")
|
2014-06-24 15:44:20 -04:00
|
|
|
}
|
|
|
|
|
2014-12-11 02:30:13 -05:00
|
|
|
func ExampleJWTConfig() {
|
|
|
|
conf := &oauth2.JWTConfig{
|
|
|
|
Email: "xxx@developer.com",
|
2014-08-16 21:04:02 -04:00
|
|
|
// The contents of your RSA private key or your PEM file
|
|
|
|
// that contains a private key.
|
|
|
|
// If you have a p12 file instead, you
|
2014-06-24 15:44:20 -04:00
|
|
|
// can use `openssl` to export the private key into a pem file.
|
2014-08-16 21:04:02 -04:00
|
|
|
//
|
|
|
|
// $ openssl pkcs12 -in key.p12 -out key.pem -nodes
|
|
|
|
//
|
|
|
|
// It only supports PEM containers with no passphrase.
|
2014-12-11 02:30:13 -05:00
|
|
|
PrivateKey: []byte("-----BEGIN RSA PRIVATE KEY-----..."),
|
|
|
|
Subject: "user@example.com",
|
|
|
|
TokenURL: "https://provider.com/o/oauth2/token",
|
2014-06-24 16:10:10 -04:00
|
|
|
}
|
2014-06-24 15:44:20 -04:00
|
|
|
// Initiate an http.Client, the following GET request will be
|
2014-11-06 19:36:41 -05:00
|
|
|
// authorized and authenticated on the behalf of user@example.com.
|
2014-12-30 16:25:01 -05:00
|
|
|
client := conf.Client(oauth2.NoContext)
|
2014-06-24 16:10:10 -04:00
|
|
|
client.Get("...")
|
2014-06-24 15:44:20 -04:00
|
|
|
}
|