Skip to content

Commit fe0eecc

Browse files
committed
Some cleanup, adding Google web flow example.
1 parent c4d44ca commit fe0eecc

File tree

3 files changed

+83
-61
lines changed

3 files changed

+83
-61
lines changed

example_test.go

+14-8
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,18 @@ func Example_config() {
2121

2222
// Redirect user to consent page to ask for permission
2323
// for the scopes specified above.
24-
url, _ := conf.AuthCodeURL("")
24+
url, err := conf.AuthCodeURL("")
25+
if err != nil {
26+
log.Fatal(err)
27+
}
2528
fmt.Printf("Visit the URL for the auth dialog: %v", url)
2629

2730
// Use the exchange code that is handled by the redirect URL.
2831
// NewTransportWithCode will do the handshake to retrieve
2932
// an access token and iniate a Transport that is
3033
// authorized and authenticated the retrieved token.
3134
var exchangeCode string
32-
fmt.Scanln(&exchangeCode)
35+
fmt.Scan(&exchangeCode)
3336
t, err := conf.NewTransportWithCode(exchangeCode)
3437
if err != nil {
3538
log.Fatal(err)
@@ -38,7 +41,7 @@ func Example_config() {
3841
// You can use t to initiate a new http.Client and
3942
// start making authenticated requests.
4043
client := http.Client{Transport: t}
41-
client.Get("https://host/path")
44+
client.Get("...")
4245

4346
// Alternatively, you can initiate a new transport
4447
// with tokens from a cache.
@@ -55,11 +58,11 @@ func Example_config() {
5558
log.Fatal(err)
5659
}
5760
client = http.Client{Transport: t}
58-
client.Get("https://host/path")
61+
client.Get("...")
5962
}
6063

6164
func Example_jWTConfig() {
62-
conf, _ := NewJWTConfig(&JWTOptions{
65+
conf, err := NewJWTConfig(&JWTOptions{
6366
Email: "xxx@developer.gserviceaccount.com",
6467
// The path to the pem file. If you have a p12 file instead, you
6568
// can use `openssl` to export the private key into a pem file.
@@ -68,18 +71,21 @@ func Example_jWTConfig() {
6871
Scopes: []string{"SCOPE1", "SCOPE2"},
6972
},
7073
"https://provider.com/o/oauth2/token")
74+
if err != nil {
75+
log.Fatal(err)
76+
}
7177

7278
// Initiate an http.Client, the following GET request will be
7379
// authorized and authenticated on the behalf of
7480
// xxx@developer.gserviceaccount.com.
7581
client := http.Client{Transport: conf.NewTransport()}
76-
client.Get("https://host/path")
82+
client.Get("...")
7783

7884
// If you would like to impersonate a user, you can
7985
// create a transport with a subject. The following GET
8086
// request will be made on the behalf of user@example.com.
8187
client = http.Client{Transport: conf.NewTransportWithUser("user@example.com")}
82-
client.Get("https://host/path")
88+
client.Get("...")
8389

8490
// Alternatively you can iniate a transport with
8591
// a token read from the cache.
@@ -93,5 +99,5 @@ func Example_jWTConfig() {
9399
client = http.Client{Transport: t}
94100
// The following request will be authorized by the token
95101
// retrieved from the cache.
96-
client.Get("https://host/path")
102+
client.Get("...")
97103
}

google/example_test.go

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package google
2+
3+
import (
4+
"log"
5+
"net/http"
6+
7+
"github.com/golang/oauth2"
8+
)
9+
10+
func Example_webServer() {
11+
// Your credentials should be obtained from the Google
12+
// Developer Console (https://console.developers.google.com).
13+
config, err := NewConfig(&oauth2.Opts{
14+
ClientID: "YOUR_CLIENT_ID",
15+
ClientSecret: "YOUR_CLIENT_SECRET",
16+
RedirectURL: "YOUR_REDIRECT_URL",
17+
Scopes: []string{
18+
"https://www.googleapis.com/auth/bigquery",
19+
"https://www.googleapis.com/auth/blogger"},
20+
})
21+
if err != nil {
22+
log.Fatal(err)
23+
}
24+
25+
// Redirect user to Google's consent page to ask for permission
26+
// for the scopes specified above.
27+
url, err := config.AuthCodeURL("")
28+
if err != nil {
29+
log.Fatal(err)
30+
}
31+
fmt.Printf("Visit the URL for the auth dialog: %v", url)
32+
33+
// Handle the exchange code to initiate a transport
34+
t, err := config.NewTransportWithCode("exchange-code")
35+
if err != nil {
36+
log.Fatal(err)
37+
}
38+
client := http.Client{Transport: t}
39+
client.Get("...")
40+
41+
// Alternatively you can initiate a new transport
42+
// with a token from a cache.
43+
cache := oauth2.NewFileCache("/path/to/file")
44+
// NewTransportWithCache will try to read the cached
45+
// token, if any error occurs, it returns the error.
46+
// If a token is available at the cache, initiates
47+
// a new transport authorized and authenticated with
48+
// the read token. If token expires, and a new access
49+
// token is retrieved, it writes the newly fetched
50+
// token to the cache.
51+
t, err = config.NewTransportWithCache(cache)
52+
if err != nil {
53+
log.Fatal(err)
54+
}
55+
client = http.Client{Transport: t}
56+
client.Get("...")
57+
}
58+
59+
func Example_serviceAccounts() {
60+
61+
}
62+
63+
func Example_appEngine() {
64+
65+
}
66+
67+
func Example_computeEngine() {
68+
69+
}

google/google.go

-53
Original file line numberDiff line numberDiff line change
@@ -11,59 +11,6 @@
1111
//
1212
// For more information, please read
1313
// https://developers.google.com/accounts/docs/OAuth2.
14-
//
15-
// Example usage:
16-
// // Web server flow usage:
17-
// // Specify your configuration.
18-
// // Your credentials should be obtained from the Google
19-
// // Developer Console (https://console.developers.google.com).
20-
// var config = google.NewConfig(&oauth2.Opts{
21-
// ClientID: YOUR_CLIENT_ID,
22-
// ClientSecret: YOUR_CLIENT_SECRET,
23-
// RedirectURL: "http://you.example.org/handler",
24-
// Scopes: []string{ "scope1", "scope2" },
25-
// })
26-
//
27-
// // A landing page redirects to Google to get the auth code.
28-
// func landing(w http.ResponseWriter, r *http.Request) {
29-
// http.Redirect(w, r, config.AuthCodeURL(""), http.StatusFound)
30-
// }
31-
//
32-
// // The user will be redirected back to this handler, that takes the
33-
// // "code" query parameter and Exchanges it for an access token.
34-
// func handler(w http.ResponseWriter, r *http.Request) {
35-
// t, err := config.NewTransportWithCode(r.FormValue("code"))
36-
// // The Transport now has a valid Token. Create an *http.Client
37-
// // with which we can make authenticated API requests.
38-
// c := t.Client()
39-
// c.Post(...)
40-
// }
41-
//
42-
// // Service accounts usage:
43-
// // Google Developer Console will provide a p12 file contains
44-
// // a private key. You need to export it to the pem format.
45-
// // Run the following command to generate a pem file that
46-
// // contains your private key:
47-
// // $ openssl pkcs12 -in /path/to/p12key.p12 -out key.pem -nodes
48-
// // Then, specify your configuration.
49-
// var config = google.NewServiceAccountConfig(&oauth2.JWTOpts{
50-
// Email: "xxx@developer.gserviceaccount.com",
51-
// PemFilename: "/path/to/key.pem",
52-
// Scopes: []string{
53-
// "https://www.googleapis.com/auth/drive.readonly"
54-
// },
55-
// })
56-
//
57-
// // Create a transport.
58-
// t, err := config.NewTransport()
59-
// // Or, you can create a transport that impersonates
60-
// // a Google user.
61-
// t, err := config.NewTransportWithUser(googleUserEmail)
62-
//
63-
// // Create a client to make authorized requests.
64-
// c := t.Client()
65-
// c.Post(...)
66-
//
6714
package google
6815

6916
import (

0 commit comments

Comments
 (0)