Add app engine and compute engine examples.

This commit is contained in:
Burcu Dogan 2014-06-24 13:27:04 -07:00
parent f156f2868e
commit 49766fd328
1 changed files with 60 additions and 2 deletions

View File

@ -5,12 +5,13 @@ import (
"net/http" "net/http"
"github.com/golang/oauth2" "github.com/golang/oauth2"
"google.golang.org/appengine"
) )
func Example_webServer() { func Example_webServer() {
// Your credentials should be obtained from the Google // Your credentials should be obtained from the Google
// Developer Console (https://console.developers.google.com). // Developer Console (https://console.developers.google.com).
config, err := NewConfig(&oauth2.Opts{ config, err := NewConfig(&oauth2.Options{
ClientID: "YOUR_CLIENT_ID", ClientID: "YOUR_CLIENT_ID",
ClientSecret: "YOUR_CLIENT_SECRET", ClientSecret: "YOUR_CLIENT_SECRET",
RedirectURL: "YOUR_REDIRECT_URL", RedirectURL: "YOUR_REDIRECT_URL",
@ -57,13 +58,70 @@ func Example_webServer() {
} }
func Example_serviceAccounts() { func Example_serviceAccounts() {
// Your credentials should be obtained from the Google
// Developer Console (https://console.developers.google.com).
config, err := NewServiceAccountConfig(&oauth2.JWTOptions{
Email: "xxx@developer.gserviceaccount.com",
// The path to the pem file. 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
PemFilename: "/path/to/pem/file.pem",
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.
client := http.Client{Transport: conf.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: conf.NewTransportWithUser("user@example.com")}
client.Get("...")
// Alternatively you can iniate a transport with
// a token read from the cache.
// If the existing access token expires, and a new access token is
// retrieved, the newly fetched token will be written to the cache.
cache := NewFileCache("/path/to/file")
t, err := conf.NewTransportWithCache(cache)
if err != nil {
log.Fatal(err)
}
client = http.Client{Transport: t}
// The following request will be authorized by the token
// retrieved from the cache.
client.Get("...")
} }
func Example_appEngine() { func Example_appEngine() {
context := appengine.NewContext(nil)
config, err := NewAppEngineConfig(context, []string{
"https://www.googleapis.com/auth/bigquery",
})
if err != nil {
log.Fatal(err)
}
// 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("...")
} }
func Example_computeEngine() { func Example_computeEngine() {
// If no other account is specified, "default" is in use.
config, err := NewComputeEngineConfig("")
if err != nil {
log.Fatal(err)
}
client := http.Client{Transport: config.NewTransport()}
client.Get("...")
} }