forked from Mirrors/oauth2
Some cleanup, adding Google web flow example.
This commit is contained in:
parent
c4d44ca3c9
commit
fe0eecc41c
|
@ -21,7 +21,10 @@ func Example_config() {
|
|||
|
||||
// Redirect user to consent page to ask for permission
|
||||
// for the scopes specified above.
|
||||
url, _ := conf.AuthCodeURL("")
|
||||
url, err := conf.AuthCodeURL("")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Printf("Visit the URL for the auth dialog: %v", url)
|
||||
|
||||
// Use the exchange code that is handled by the redirect URL.
|
||||
|
@ -29,7 +32,7 @@ func Example_config() {
|
|||
// an access token and iniate a Transport that is
|
||||
// authorized and authenticated the retrieved token.
|
||||
var exchangeCode string
|
||||
fmt.Scanln(&exchangeCode)
|
||||
fmt.Scan(&exchangeCode)
|
||||
t, err := conf.NewTransportWithCode(exchangeCode)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
|
@ -38,7 +41,7 @@ func Example_config() {
|
|||
// You can use t to initiate a new http.Client and
|
||||
// start making authenticated requests.
|
||||
client := http.Client{Transport: t}
|
||||
client.Get("https://host/path")
|
||||
client.Get("...")
|
||||
|
||||
// Alternatively, you can initiate a new transport
|
||||
// with tokens from a cache.
|
||||
|
@ -55,11 +58,11 @@ func Example_config() {
|
|||
log.Fatal(err)
|
||||
}
|
||||
client = http.Client{Transport: t}
|
||||
client.Get("https://host/path")
|
||||
client.Get("...")
|
||||
}
|
||||
|
||||
func Example_jWTConfig() {
|
||||
conf, _ := NewJWTConfig(&JWTOptions{
|
||||
conf, err := NewJWTConfig(&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.
|
||||
|
@ -68,18 +71,21 @@ func Example_jWTConfig() {
|
|||
Scopes: []string{"SCOPE1", "SCOPE2"},
|
||||
},
|
||||
"https://provider.com/o/oauth2/token")
|
||||
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("https://host/path")
|
||||
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("https://host/path")
|
||||
client.Get("...")
|
||||
|
||||
// Alternatively you can iniate a transport with
|
||||
// a token read from the cache.
|
||||
|
@ -93,5 +99,5 @@ func Example_jWTConfig() {
|
|||
client = http.Client{Transport: t}
|
||||
// The following request will be authorized by the token
|
||||
// retrieved from the cache.
|
||||
client.Get("https://host/path")
|
||||
client.Get("...")
|
||||
}
|
||||
|
|
|
@ -0,0 +1,69 @@
|
|||
package google
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/golang/oauth2"
|
||||
)
|
||||
|
||||
func Example_webServer() {
|
||||
// Your credentials should be obtained from the Google
|
||||
// Developer Console (https://console.developers.google.com).
|
||||
config, err := NewConfig(&oauth2.Opts{
|
||||
ClientID: "YOUR_CLIENT_ID",
|
||||
ClientSecret: "YOUR_CLIENT_SECRET",
|
||||
RedirectURL: "YOUR_REDIRECT_URL",
|
||||
Scopes: []string{
|
||||
"https://www.googleapis.com/auth/bigquery",
|
||||
"https://www.googleapis.com/auth/blogger"},
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Redirect user to Google's consent page to ask for permission
|
||||
// for the scopes specified above.
|
||||
url, err := config.AuthCodeURL("")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Printf("Visit the URL for the auth dialog: %v", url)
|
||||
|
||||
// Handle the exchange code to initiate a transport
|
||||
t, err := config.NewTransportWithCode("exchange-code")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
client := http.Client{Transport: t}
|
||||
client.Get("...")
|
||||
|
||||
// Alternatively you can initiate a new transport
|
||||
// with a token from a cache.
|
||||
cache := oauth2.NewFileCache("/path/to/file")
|
||||
// NewTransportWithCache will try to read the cached
|
||||
// token, if any error occurs, it returns the error.
|
||||
// If a token is available at the cache, initiates
|
||||
// a new transport authorized and authenticated with
|
||||
// the read token. If token expires, and a new access
|
||||
// token is retrieved, it writes the newly fetched
|
||||
// token to the cache.
|
||||
t, err = config.NewTransportWithCache(cache)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
client = http.Client{Transport: t}
|
||||
client.Get("...")
|
||||
}
|
||||
|
||||
func Example_serviceAccounts() {
|
||||
|
||||
}
|
||||
|
||||
func Example_appEngine() {
|
||||
|
||||
}
|
||||
|
||||
func Example_computeEngine() {
|
||||
|
||||
}
|
|
@ -11,59 +11,6 @@
|
|||
//
|
||||
// For more information, please read
|
||||
// https://developers.google.com/accounts/docs/OAuth2.
|
||||
//
|
||||
// Example usage:
|
||||
// // Web server flow usage:
|
||||
// // Specify your configuration.
|
||||
// // Your credentials should be obtained from the Google
|
||||
// // Developer Console (https://console.developers.google.com).
|
||||
// var config = google.NewConfig(&oauth2.Opts{
|
||||
// ClientID: YOUR_CLIENT_ID,
|
||||
// ClientSecret: YOUR_CLIENT_SECRET,
|
||||
// RedirectURL: "http://you.example.org/handler",
|
||||
// Scopes: []string{ "scope1", "scope2" },
|
||||
// })
|
||||
//
|
||||
// // A landing page redirects to Google to get the auth code.
|
||||
// func landing(w http.ResponseWriter, r *http.Request) {
|
||||
// http.Redirect(w, r, config.AuthCodeURL(""), http.StatusFound)
|
||||
// }
|
||||
//
|
||||
// // The user will be redirected back to this handler, that takes the
|
||||
// // "code" query parameter and Exchanges it for an access token.
|
||||
// func handler(w http.ResponseWriter, r *http.Request) {
|
||||
// t, err := config.NewTransportWithCode(r.FormValue("code"))
|
||||
// // The Transport now has a valid Token. Create an *http.Client
|
||||
// // with which we can make authenticated API requests.
|
||||
// c := t.Client()
|
||||
// c.Post(...)
|
||||
// }
|
||||
//
|
||||
// // Service accounts usage:
|
||||
// // Google Developer Console will provide a p12 file contains
|
||||
// // a private key. You need to export it to the pem format.
|
||||
// // Run the following command to generate a pem file that
|
||||
// // contains your private key:
|
||||
// // $ openssl pkcs12 -in /path/to/p12key.p12 -out key.pem -nodes
|
||||
// // Then, specify your configuration.
|
||||
// var config = google.NewServiceAccountConfig(&oauth2.JWTOpts{
|
||||
// Email: "xxx@developer.gserviceaccount.com",
|
||||
// PemFilename: "/path/to/key.pem",
|
||||
// Scopes: []string{
|
||||
// "https://www.googleapis.com/auth/drive.readonly"
|
||||
// },
|
||||
// })
|
||||
//
|
||||
// // Create a transport.
|
||||
// t, err := config.NewTransport()
|
||||
// // Or, you can create a transport that impersonates
|
||||
// // a Google user.
|
||||
// t, err := config.NewTransportWithUser(googleUserEmail)
|
||||
//
|
||||
// // Create a client to make authorized requests.
|
||||
// c := t.Client()
|
||||
// c.Post(...)
|
||||
//
|
||||
package google
|
||||
|
||||
import (
|
||||
|
|
Loading…
Reference in New Issue