Fix examples.

This commit is contained in:
Burcu Dogan 2014-06-24 14:28:46 -07:00
parent 49766fd328
commit cb989650ab
2 changed files with 25 additions and 22 deletions

View File

@ -1,13 +1,18 @@
package oauth2 package oauth2_test
import ( import (
"fmt" "fmt"
"log" "log"
"net/http" "net/http"
"testing"
"github.com/golang/oauth2"
) )
func TestA(t *testing.T) {}
func Example_config() { func Example_config() {
conf, err := NewConfig(&Options{ conf, err := oauth2.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",
@ -47,7 +52,7 @@ func Example_config() {
// Alternatively, you can initiate a new transport // Alternatively, you can initiate a new transport
// with tokens from a cache. // with tokens from a cache.
cache := NewFileCache("/path/to/file") cache := oauth2.NewFileCache("/path/to/file")
// NewTransportWithCache will try to read the cached // NewTransportWithCache will try to read the cached
// token, if any error occurs, it returns the error. // token, if any error occurs, it returns the error.
// If a token is available at the cache, initiates // If a token is available at the cache, initiates
@ -64,7 +69,7 @@ func Example_config() {
} }
func Example_jWTConfig() { func Example_jWTConfig() {
conf, err := NewJWTConfig(&JWTOptions{ conf, err := oauth2.NewJWTConfig(&oauth2.JWTOptions{
Email: "xxx@developer.gserviceaccount.com", Email: "xxx@developer.gserviceaccount.com",
// The path to the pem file. If you have a p12 file instead, you // 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. // can use `openssl` to export the private key into a pem file.
@ -93,7 +98,7 @@ func Example_jWTConfig() {
// a token read from the cache. // a token read from the cache.
// If the existing access token expires, and a new access token is // If the existing access token expires, and a new access token is
// retrieved, the newly fetched token will be written to the cache. // retrieved, the newly fetched token will be written to the cache.
cache := NewFileCache("/path/to/file") cache := oauth2.NewFileCache("/path/to/file")
t, err := conf.NewTransportWithCache(cache) t, err := conf.NewTransportWithCache(cache)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)

View File

@ -1,17 +1,22 @@
package google package google_test
import ( import (
"fmt"
"log" "log"
"net/http" "net/http"
"testing"
"github.com/golang/oauth2" "github.com/golang/oauth2"
"github.com/golang/oauth2/google"
"google.golang.org/appengine" "google.golang.org/appengine"
) )
func TestA(t *testing.T) {}
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.Options{ config, err := google.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",
@ -60,7 +65,7 @@ func Example_webServer() {
func Example_serviceAccounts() { func Example_serviceAccounts() {
// 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 := NewServiceAccountConfig(&oauth2.JWTOptions{ config, err := google.NewServiceAccountConfig(&oauth2.JWTOptions{
Email: "xxx@developer.gserviceaccount.com", Email: "xxx@developer.gserviceaccount.com",
// The path to the pem file. If you have a p12 file instead, you // 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. // can use `openssl` to export the private key into a pem file.
@ -77,21 +82,21 @@ func Example_serviceAccounts() {
// Initiate an http.Client, the following GET request will be // Initiate an http.Client, the following GET request will be
// authorized and authenticated on the behalf of // authorized and authenticated on the behalf of
// xxx@developer.gserviceaccount.com. // xxx@developer.gserviceaccount.com.
client := http.Client{Transport: conf.NewTransport()} client := http.Client{Transport: config.NewTransport()}
client.Get("...") client.Get("...")
// If you would like to impersonate a user, you can // If you would like to impersonate a user, you can
// create a transport with a subject. The following GET // create a transport with a subject. The following GET
// request will be made on the behalf of user@example.com. // request will be made on the behalf of user@example.com.
client = http.Client{Transport: conf.NewTransportWithUser("user@example.com")} client = http.Client{Transport: config.NewTransportWithUser("user@example.com")}
client.Get("...") client.Get("...")
// Alternatively you can iniate a transport with // Alternatively you can iniate a transport with
// a token read from the cache. // a token read from the cache.
// If the existing access token expires, and a new access token is // If the existing access token expires, and a new access token is
// retrieved, the newly fetched token will be written to the cache. // retrieved, the newly fetched token will be written to the cache.
cache := NewFileCache("/path/to/file") cache := oauth2.NewFileCache("/path/to/file")
t, err := conf.NewTransportWithCache(cache) t, err := config.NewTransportWithCache(cache)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -103,13 +108,9 @@ func Example_serviceAccounts() {
func Example_appEngine() { func Example_appEngine() {
context := appengine.NewContext(nil) context := appengine.NewContext(nil)
config, err := NewAppEngineConfig(context, []string{ config := google.NewAppEngineConfig(context, []string{
"https://www.googleapis.com/auth/bigquery", "https://www.googleapis.com/auth/bigquery",
}) })
if err != nil {
log.Fatal(err)
}
// The following client will be authorized by the App Engine // The following client will be authorized by the App Engine
// app's service account for the provided scopes. // app's service account for the provided scopes.
client := http.Client{Transport: config.NewTransport()} client := http.Client{Transport: config.NewTransport()}
@ -117,11 +118,8 @@ func Example_appEngine() {
} }
func Example_computeEngine() { func Example_computeEngine() {
// If no other account is specified, "default" is in use. // If no other account is specified, "default" is used.
config, err := NewComputeEngineConfig("") config := google.NewComputeEngineConfig("")
if err != nil {
log.Fatal(err)
}
client := http.Client{Transport: config.NewTransport()} client := http.Client{Transport: config.NewTransport()}
client.Get("...") client.Get("...")
} }