2014-06-24 17:28:46 -04:00
|
|
|
package oauth2_test
|
2014-06-24 15:44:20 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2014-06-24 17:28:46 -04:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/golang/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-06-24 15:44:20 -04:00
|
|
|
func Example_config() {
|
2014-06-24 17:28:46 -04:00
|
|
|
conf, err := oauth2.NewConfig(&oauth2.Options{
|
2014-06-24 15:44:20 -04:00
|
|
|
ClientID: "YOUR_CLIENT_ID",
|
|
|
|
ClientSecret: "YOUR_CLIENT_SECRET",
|
|
|
|
RedirectURL: "YOUR_REDIRECT_URL",
|
|
|
|
Scopes: []string{"SCOPE1", "SCOPE2"},
|
|
|
|
},
|
|
|
|
"https://provider.com/o/oauth2/auth",
|
|
|
|
"https://provider.com/o/oauth2/token")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Redirect user to consent page to ask for permission
|
|
|
|
// for the scopes specified above.
|
2014-09-05 01:43:23 -04:00
|
|
|
url := conf.AuthCodeURL("state", "online", "auto")
|
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.
|
|
|
|
var authorizationCode string
|
|
|
|
if _, err = fmt.Scan(&authorizationCode); err != nil {
|
2014-06-24 16:26:45 -04:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2014-08-16 22:29:11 -04:00
|
|
|
t, err := conf.NewTransportWithCode(authorizationCode)
|
2014-06-24 15:44:20 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// You can use t to initiate a new http.Client and
|
|
|
|
// start making authenticated requests.
|
|
|
|
client := http.Client{Transport: t}
|
2014-06-24 16:10:10 -04:00
|
|
|
client.Get("...")
|
2014-06-24 15:44:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func Example_jWTConfig() {
|
2014-06-24 17:28:46 -04:00
|
|
|
conf, err := oauth2.NewJWTConfig(&oauth2.JWTOptions{
|
2014-06-24 15:44:20 -04:00
|
|
|
Email: "xxx@developer.gserviceaccount.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.
|
|
|
|
PrivateKey: []byte("PRIVATE KEY CONTENTS"),
|
|
|
|
Scopes: []string{"SCOPE1", "SCOPE2"},
|
2014-06-24 15:44:20 -04:00
|
|
|
},
|
|
|
|
"https://provider.com/o/oauth2/token")
|
2014-06-24 16:10:10 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2014-06-24 15:44:20 -04:00
|
|
|
|
|
|
|
// Initiate an http.Client, the following GET request will be
|
|
|
|
// authorized and authenticated on the behalf of
|
|
|
|
// xxx@developer.gserviceaccount.com.
|
|
|
|
client := http.Client{Transport: conf.NewTransport()}
|
2014-06-24 16:10:10 -04:00
|
|
|
client.Get("...")
|
2014-06-24 15:44:20 -04:00
|
|
|
|
|
|
|
// If you would like to impersonate a user, you can
|
|
|
|
// create a transport with a subject. The following GET
|
|
|
|
// request will be made on the behalf of user@example.com.
|
|
|
|
client = http.Client{Transport: conf.NewTransportWithUser("user@example.com")}
|
2014-06-24 16:10:10 -04:00
|
|
|
client.Get("...")
|
2014-06-24 15:44:20 -04:00
|
|
|
}
|