| OLD | NEW |
| 1 // Copyright 2014 The oauth2 Authors. All rights reserved. | 1 // Copyright 2014 The oauth2 Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
| 3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package oauth2_test | 5 package oauth2_test |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "fmt" | 8 "fmt" |
| 9 "log" | 9 "log" |
| 10 "net/http" | 10 "net/http" |
| 11 "testing" | 11 "testing" |
| 12 | 12 |
| 13 "github.com/golang/oauth2" | 13 "github.com/golang/oauth2" |
| 14 ) | 14 ) |
| 15 | 15 |
| 16 // TODO(jbd): Remove after Go 1.4. | 16 // TODO(jbd): Remove after Go 1.4. |
| 17 // Related to https://codereview.appspot.com/107320046 | 17 // Related to https://codereview.appspot.com/107320046 |
| 18 func TestA(t *testing.T) {} | 18 func TestA(t *testing.T) {} |
| 19 | 19 |
| 20 func Example_regular() { | 20 func Example_regular() { |
| 21 » f, err := oauth2.New( | 21 » opts, err := oauth2.New( |
| 22 oauth2.Client("YOUR_CLIENT_ID", "YOUR_CLIENT_SECRET"), | 22 oauth2.Client("YOUR_CLIENT_ID", "YOUR_CLIENT_SECRET"), |
| 23 oauth2.RedirectURL("YOUR_REDIRECT_URL"), | 23 oauth2.RedirectURL("YOUR_REDIRECT_URL"), |
| 24 oauth2.Scope("SCOPE1", "SCOPE2"), | 24 oauth2.Scope("SCOPE1", "SCOPE2"), |
| 25 oauth2.Endpoint( | 25 oauth2.Endpoint( |
| 26 "https://provider.com/o/oauth2/auth", | 26 "https://provider.com/o/oauth2/auth", |
| 27 "https://provider.com/o/oauth2/token", | 27 "https://provider.com/o/oauth2/token", |
| 28 ), | 28 ), |
| 29 ) | 29 ) |
| 30 if err != nil { | 30 if err != nil { |
| 31 log.Fatal(err) | 31 log.Fatal(err) |
| 32 } | 32 } |
| 33 | 33 |
| 34 // Redirect user to consent page to ask for permission | 34 // Redirect user to consent page to ask for permission |
| 35 // for the scopes specified above. | 35 // for the scopes specified above. |
| 36 » url := f.AuthCodeURL("state", "online", "auto") | 36 » url := opts.AuthCodeURL("state", "online", "auto") |
| 37 fmt.Printf("Visit the URL for the auth dialog: %v", url) | 37 fmt.Printf("Visit the URL for the auth dialog: %v", url) |
| 38 | 38 |
| 39 // Use the authorization code that is pushed to the redirect URL. | 39 // Use the authorization code that is pushed to the redirect URL. |
| 40 // NewTransportWithCode will do the handshake to retrieve | 40 // NewTransportWithCode will do the handshake to retrieve |
| 41 // an access token and initiate a Transport that is | 41 // an access token and initiate a Transport that is |
| 42 // authorized and authenticated by the retrieved token. | 42 // authorized and authenticated by the retrieved token. |
| 43 var code string | 43 var code string |
| 44 if _, err = fmt.Scan(&code); err != nil { | 44 if _, err = fmt.Scan(&code); err != nil { |
| 45 log.Fatal(err) | 45 log.Fatal(err) |
| 46 } | 46 } |
| 47 » t, err := f.NewTransportFromCode(code) | 47 » t, err := opts.NewTransportFromCode(code) |
| 48 if err != nil { | 48 if err != nil { |
| 49 log.Fatal(err) | 49 log.Fatal(err) |
| 50 } | 50 } |
| 51 | 51 |
| 52 // You can use t to initiate a new http.Client and | 52 // You can use t to initiate a new http.Client and |
| 53 // start making authenticated requests. | 53 // start making authenticated requests. |
| 54 client := http.Client{Transport: t} | 54 client := http.Client{Transport: t} |
| 55 client.Get("...") | 55 client.Get("...") |
| 56 } | 56 } |
| 57 | 57 |
| 58 func Example_jWT() { | 58 func Example_jWT() { |
| 59 » f, err := oauth2.New( | 59 » opts, err := oauth2.New( |
| 60 // The contents of your RSA private key or your PEM file | 60 // The contents of your RSA private key or your PEM file |
| 61 // that contains a private key. | 61 // that contains a private key. |
| 62 // If you have a p12 file instead, you | 62 // If you have a p12 file instead, you |
| 63 // can use `openssl` to export the private key into a pem file. | 63 // can use `openssl` to export the private key into a pem file. |
| 64 // | 64 // |
| 65 // $ openssl pkcs12 -in key.p12 -out key.pem -nodes | 65 // $ openssl pkcs12 -in key.p12 -out key.pem -nodes |
| 66 // | 66 // |
| 67 // It only supports PEM containers with no passphrase. | 67 // It only supports PEM containers with no passphrase. |
| 68 oauth2.JWTClient( | 68 oauth2.JWTClient( |
| 69 "xxx@developer.gserviceaccount.com", | 69 "xxx@developer.gserviceaccount.com", |
| 70 []byte("-----BEGIN RSA PRIVATE KEY-----...")), | 70 []byte("-----BEGIN RSA PRIVATE KEY-----...")), |
| 71 oauth2.Scope("SCOPE1", "SCOPE2"), | 71 oauth2.Scope("SCOPE1", "SCOPE2"), |
| 72 oauth2.JWTEndpoint("https://provider.com/o/oauth2/token"), | 72 oauth2.JWTEndpoint("https://provider.com/o/oauth2/token"), |
| 73 // If you would like to impersonate a user, you can | 73 // If you would like to impersonate a user, you can |
| 74 // create a transport with a subject. The following GET | 74 // create a transport with a subject. The following GET |
| 75 // request will be made on the behalf of user@example.com. | 75 // request will be made on the behalf of user@example.com. |
| 76 // Subject is optional. | 76 // Subject is optional. |
| 77 oauth2.Subject("user@example.com"), | 77 oauth2.Subject("user@example.com"), |
| 78 ) | 78 ) |
| 79 if err != nil { | 79 if err != nil { |
| 80 log.Fatal(err) | 80 log.Fatal(err) |
| 81 } | 81 } |
| 82 | 82 |
| 83 // Initiate an http.Client, the following GET request will be | 83 // Initiate an http.Client, the following GET request will be |
| 84 // authorized and authenticated on the behalf of user@example.com. | 84 // authorized and authenticated on the behalf of user@example.com. |
| 85 » client := http.Client{Transport: f.NewTransport()} | 85 » client := http.Client{Transport: opts.NewTransport()} |
| 86 client.Get("...") | 86 client.Get("...") |
| 87 } | 87 } |
| OLD | NEW |