2014-09-03 14:50:43 -04: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 google_test
|
2014-06-24 16:10:10 -04:00
|
|
|
|
|
|
|
import (
|
2014-06-24 17:28:46 -04:00
|
|
|
"fmt"
|
2014-06-24 16:10:10 -04:00
|
|
|
"log"
|
|
|
|
"net/http"
|
2014-06-24 17:28:46 -04:00
|
|
|
"testing"
|
2014-06-24 16:10:10 -04:00
|
|
|
|
|
|
|
"github.com/golang/oauth2"
|
2014-06-24 17:28:46 -04:00
|
|
|
"github.com/golang/oauth2/google"
|
2014-06-24 16:27:04 -04:00
|
|
|
"google.golang.org/appengine"
|
2014-06-24 16:10:10 -04:00
|
|
|
)
|
|
|
|
|
2014-06-24 17:53:39 -04:00
|
|
|
// 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 16:10:10 -04:00
|
|
|
func Example_webServer() {
|
|
|
|
// Your credentials should be obtained from the Google
|
|
|
|
// Developer Console (https://console.developers.google.com).
|
2014-06-24 17:28:46 -04:00
|
|
|
config, err := google.NewConfig(&oauth2.Options{
|
2014-06-24 16:10:10 -04:00
|
|
|
ClientID: "YOUR_CLIENT_ID",
|
|
|
|
ClientSecret: "YOUR_CLIENT_SECRET",
|
|
|
|
RedirectURL: "YOUR_REDIRECT_URL",
|
|
|
|
Scopes: []string{
|
|
|
|
"https://www.googleapis.com/auth/bigquery",
|
|
|
|
"https://www.googleapis.com/auth/blogger"},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Redirect user to Google's consent page to ask for permission
|
|
|
|
// for the scopes specified above.
|
2014-09-05 01:43:23 -04:00
|
|
|
url := config.AuthCodeURL("state", "online", "auto")
|
2014-06-24 16:10:10 -04:00
|
|
|
fmt.Printf("Visit the URL for the auth dialog: %v", url)
|
|
|
|
|
|
|
|
// Handle the exchange code to initiate a transport
|
|
|
|
t, err := config.NewTransportWithCode("exchange-code")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
client := http.Client{Transport: t}
|
|
|
|
client.Get("...")
|
|
|
|
}
|
|
|
|
|
2014-09-03 14:40:47 -04:00
|
|
|
func Example_serviceAccountsJSON() {
|
|
|
|
// Your credentials should be obtained from the Google
|
|
|
|
// Developer Console (https://console.developers.google.com).
|
|
|
|
// Navigate to your project, then see the "Credentials" page
|
|
|
|
// under "APIs & Auth".
|
|
|
|
// To create a service account client, click "Create new Client ID",
|
|
|
|
// select "Service Account", and click "Create Client ID". A JSON
|
|
|
|
// key file will then be downloaded to your computer.
|
|
|
|
config, err := google.NewServiceAccountJSONConfig(
|
|
|
|
"/path/to/your-project-key.json",
|
|
|
|
"https://www.googleapis.com/auth/bigquery",
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
// Initiate an http.Client. The following GET request will be
|
|
|
|
// authorized and authenticated on the behalf of
|
|
|
|
// your service account.
|
|
|
|
client := http.Client{Transport: config.NewTransport()}
|
|
|
|
client.Get("...")
|
|
|
|
|
|
|
|
// 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: config.NewTransportWithUser("user@example.com")}
|
|
|
|
client.Get("...")
|
|
|
|
}
|
|
|
|
|
2014-06-24 16:10:10 -04:00
|
|
|
func Example_serviceAccounts() {
|
2014-06-24 16:27:04 -04:00
|
|
|
// Your credentials should be obtained from the Google
|
|
|
|
// Developer Console (https://console.developers.google.com).
|
2014-06-24 17:28:46 -04:00
|
|
|
config, err := google.NewServiceAccountConfig(&oauth2.JWTOptions{
|
2014-06-24 16:27:04 -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
|
|
|
|
// can use `openssl` to export the private key into a PEM file.
|
|
|
|
//
|
|
|
|
// $ openssl pkcs12 -in key.p12 -out key.pem -nodes
|
|
|
|
//
|
|
|
|
// Supports only PEM containers without a passphrase.
|
|
|
|
PrivateKey: []byte("PRIVATE KEY CONTENTS"),
|
2014-06-24 16:27:04 -04:00
|
|
|
Scopes: []string{
|
|
|
|
"https://www.googleapis.com/auth/bigquery",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initiate an http.Client, the following GET request will be
|
|
|
|
// authorized and authenticated on the behalf of
|
|
|
|
// xxx@developer.gserviceaccount.com.
|
2014-06-24 17:28:46 -04:00
|
|
|
client := http.Client{Transport: config.NewTransport()}
|
2014-06-24 16:27:04 -04:00
|
|
|
client.Get("...")
|
2014-06-24 16:10:10 -04:00
|
|
|
|
2014-06-24 16:27:04 -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.
|
2014-06-24 17:28:46 -04:00
|
|
|
client = http.Client{Transport: config.NewTransportWithUser("user@example.com")}
|
2014-06-24 16:27:04 -04:00
|
|
|
client.Get("...")
|
2014-06-24 16:10:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func Example_appEngine() {
|
2014-09-29 17:38:10 -04:00
|
|
|
c := appengine.NewContext(nil)
|
|
|
|
config := google.NewAppEngineConfig(c, "https://www.googleapis.com/auth/bigquery")
|
2014-06-24 16:27:04 -04:00
|
|
|
// The following client will be authorized by the App Engine
|
|
|
|
// app's service account for the provided scopes.
|
|
|
|
client := http.Client{Transport: config.NewTransport()}
|
|
|
|
client.Get("...")
|
2014-06-24 16:10:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func Example_computeEngine() {
|
2014-06-24 17:28:46 -04:00
|
|
|
// If no other account is specified, "default" is used.
|
|
|
|
config := google.NewComputeEngineConfig("")
|
2014-06-24 16:27:04 -04:00
|
|
|
client := http.Client{Transport: config.NewTransport()}
|
|
|
|
client.Get("...")
|
2014-06-24 16:10:10 -04:00
|
|
|
}
|