2015-08-26 20:42:02 -04:00
|
|
|
// Copyright 2014 The Go Authors. All rights reserved.
|
2014-09-03 14:50:43 -04:00
|
|
|
// 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-12-11 02:30:13 -05:00
|
|
|
"io/ioutil"
|
2014-06-24 16:10:10 -04:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
|
2018-03-09 08:25:59 -05:00
|
|
|
"golang.org/x/net/context"
|
2014-11-26 14:44:45 -05:00
|
|
|
"golang.org/x/oauth2"
|
|
|
|
"golang.org/x/oauth2/google"
|
2014-12-30 18:11:02 -05:00
|
|
|
"golang.org/x/oauth2/jwt"
|
2014-06-24 16:27:04 -04:00
|
|
|
"google.golang.org/appengine"
|
2014-12-11 02:30:13 -05:00
|
|
|
"google.golang.org/appengine/urlfetch"
|
2014-06-24 16:10:10 -04:00
|
|
|
)
|
|
|
|
|
2015-02-26 00:39:41 -05:00
|
|
|
func ExampleDefaultClient() {
|
|
|
|
client, err := google.DefaultClient(oauth2.NoContext,
|
|
|
|
"https://www.googleapis.com/auth/devstorage.full_control")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
client.Get("...")
|
|
|
|
}
|
|
|
|
|
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-12-11 02:30:13 -05:00
|
|
|
conf := &oauth2.Config{
|
|
|
|
ClientID: "YOUR_CLIENT_ID",
|
|
|
|
ClientSecret: "YOUR_CLIENT_SECRET",
|
|
|
|
RedirectURL: "YOUR_REDIRECT_URL",
|
|
|
|
Scopes: []string{
|
2014-06-24 16:10:10 -04:00
|
|
|
"https://www.googleapis.com/auth/bigquery",
|
2014-11-06 19:36:41 -05:00
|
|
|
"https://www.googleapis.com/auth/blogger",
|
2014-12-11 02:30:13 -05:00
|
|
|
},
|
|
|
|
Endpoint: google.Endpoint,
|
2014-06-24 16:10:10 -04:00
|
|
|
}
|
|
|
|
// Redirect user to Google's consent page to ask for permission
|
|
|
|
// for the scopes specified above.
|
2014-12-11 02:30:13 -05:00
|
|
|
url := conf.AuthCodeURL("state")
|
2014-06-24 16:10:10 -04:00
|
|
|
fmt.Printf("Visit the URL for the auth dialog: %v", url)
|
|
|
|
|
2014-12-11 02:30:13 -05:00
|
|
|
// Handle the exchange code to initiate a transport.
|
|
|
|
tok, err := conf.Exchange(oauth2.NoContext, "authorization-code")
|
2014-06-24 16:10:10 -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-12-11 02:30:13 -05:00
|
|
|
func ExampleJWTConfigFromJSON() {
|
2014-09-03 14:40:47 -04:00
|
|
|
// 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.
|
2014-12-11 02:30:13 -05:00
|
|
|
data, err := ioutil.ReadFile("/path/to/your-project-key.json")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2015-01-13 18:27:09 -05:00
|
|
|
conf, err := google.JWTConfigFromJSON(data, "https://www.googleapis.com/auth/bigquery")
|
2014-09-03 14:40:47 -04:00
|
|
|
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.
|
2014-12-30 16:25:01 -05:00
|
|
|
client := conf.Client(oauth2.NoContext)
|
2014-09-03 14:40:47 -04:00
|
|
|
client.Get("...")
|
|
|
|
}
|
|
|
|
|
2014-12-16 14:15:52 -05:00
|
|
|
func ExampleSDKConfig() {
|
|
|
|
// The credentials will be obtained from the first account that
|
|
|
|
// has been authorized with `gcloud auth login`.
|
|
|
|
conf, err := google.NewSDKConfig("")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
// Initiate an http.Client. The following GET request will be
|
|
|
|
// authorized and authenticated on the behalf of the SDK user.
|
|
|
|
client := conf.Client(oauth2.NoContext)
|
|
|
|
client.Get("...")
|
|
|
|
}
|
|
|
|
|
2014-12-11 02:30:13 -05:00
|
|
|
func Example_serviceAccount() {
|
2014-06-24 16:27:04 -04:00
|
|
|
// Your credentials should be obtained from the Google
|
|
|
|
// Developer Console (https://console.developers.google.com).
|
2014-12-30 18:11:02 -05:00
|
|
|
conf := &jwt.Config{
|
2014-12-11 02:30:13 -05: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-11-06 19:36:41 -05:00
|
|
|
// can use `openssl` to export the private key into a pem file.
|
2014-08-16 21:04:02 -04:00
|
|
|
//
|
2014-12-16 14:04:53 -05:00
|
|
|
// $ openssl pkcs12 -in key.p12 -passin pass:notasecret -out key.pem -nodes
|
2014-08-16 21:04:02 -04:00
|
|
|
//
|
2014-12-16 14:04:53 -05:00
|
|
|
// The field only supports PEM containers with no passphrase.
|
|
|
|
// The openssl command will convert p12 keys to passphrase-less PEM containers.
|
2014-12-11 02:30:13 -05:00
|
|
|
PrivateKey: []byte("-----BEGIN RSA PRIVATE KEY-----..."),
|
|
|
|
Scopes: []string{
|
2014-06-24 16:27:04 -04:00
|
|
|
"https://www.googleapis.com/auth/bigquery",
|
2014-11-06 19:36:41 -05:00
|
|
|
"https://www.googleapis.com/auth/blogger",
|
2014-12-11 02:30:13 -05:00
|
|
|
},
|
|
|
|
TokenURL: google.JWTTokenURL,
|
2014-11-06 19:36:41 -05: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-12-11 02:30:13 -05:00
|
|
|
// Optional.
|
|
|
|
Subject: "user@example.com",
|
2014-06-24 16:27:04 -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:27:04 -04:00
|
|
|
client.Get("...")
|
2014-06-24 16:10:10 -04:00
|
|
|
}
|
|
|
|
|
2014-12-11 02:30:13 -05:00
|
|
|
func ExampleAppEngineTokenSource() {
|
|
|
|
var req *http.Request // from the ServeHTTP handler
|
|
|
|
ctx := appengine.NewContext(req)
|
|
|
|
client := &http.Client{
|
|
|
|
Transport: &oauth2.Transport{
|
|
|
|
Source: google.AppEngineTokenSource(ctx, "https://www.googleapis.com/auth/bigquery"),
|
|
|
|
Base: &urlfetch.Transport{
|
|
|
|
Context: ctx,
|
|
|
|
},
|
|
|
|
},
|
2014-11-06 19:36:41 -05:00
|
|
|
}
|
2014-06-24 16:27:04 -04:00
|
|
|
client.Get("...")
|
2014-06-24 16:10:10 -04:00
|
|
|
}
|
|
|
|
|
2014-12-11 02:30:13 -05:00
|
|
|
func ExampleComputeTokenSource() {
|
|
|
|
client := &http.Client{
|
|
|
|
Transport: &oauth2.Transport{
|
|
|
|
// Fetch from Google Compute Engine's metadata server to retrieve
|
|
|
|
// an access token for the provided account.
|
|
|
|
// If no account is specified, "default" is used.
|
|
|
|
Source: google.ComputeTokenSource(""),
|
|
|
|
},
|
2014-11-06 19:36:41 -05:00
|
|
|
}
|
2014-06-24 16:27:04 -04:00
|
|
|
client.Get("...")
|
2014-06-24 16:10:10 -04:00
|
|
|
}
|
2018-03-09 08:25:59 -05:00
|
|
|
|
|
|
|
func ExampleCredentialsFromJSON() {
|
|
|
|
ctx := context.Background()
|
|
|
|
data, err := ioutil.ReadFile("/path/to/key-file.json")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
creds, err := google.CredentialsFromJSON(ctx, data, "https://www.googleapis.com/auth/bigquery")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
_ = creds // TODO: Use creds.
|
|
|
|
}
|