GCE setup tool: more graceful OAuth setup

Change-Id: I28893735c49da18d3890a0df07b071048c29ba68
This commit is contained in:
Brad Fitzpatrick 2014-08-15 12:47:05 -07:00
parent 77d36bf198
commit bb8fe1aa33
2 changed files with 20 additions and 10 deletions

2
misc/gce/.gitignore vendored
View File

@ -1,3 +1,3 @@
client-id.dat
client-secret.dat
auth-code.dat
token.dat

View File

@ -1,6 +1,7 @@
package main
import (
"bufio"
"flag"
"io/ioutil"
"log"
@ -26,6 +27,8 @@ func readFile(v string) string {
}
var config = &oauth.Config{
// The client-id and secret should be for an "Installed Application" when using
// the CLI. Later we'll use a web application with a callback.
ClientId: readFile("client-id.dat"),
ClientSecret: readFile("client-secret.dat"),
Scope: strings.Join([]string{
@ -49,19 +52,26 @@ func main() {
imageURL := "https://www.googleapis.com/compute/v1/projects/coreos-cloud/global/images/coreos-alpha-402-2-0-v20140807"
machType := prefix + "/zones/" + *zoneFlag + "/machineTypes/" + *machFlag
if _, err := os.Stat("auth-code.dat"); err != nil {
log.Printf("No auth code in file auth-code.dat. Get auth code from %v", config.AuthCodeURL("my-state"))
return
}
authCode := readFile("auth-code.dat")
tr := &oauth.Transport{
Config: config,
}
t, err := tr.Exchange(authCode)
tokenCache := oauth.CacheFile("token.dat")
token, err := tokenCache.Token()
if err != nil {
log.Fatal(err)
log.Printf("Error getting token from %s: %v", string(tokenCache), err)
log.Printf("Get auth code from %v", config.AuthCodeURL("my-state"))
os.Stdout.Write([]byte("\nEnter auth code: "))
sc := bufio.NewScanner(os.Stdin)
sc.Scan()
authCode := strings.TrimSpace(sc.Text())
token, err = tr.Exchange(authCode)
if err != nil {
log.Fatalf("Error exchanging auth code for a token: %v", err)
}
tokenCache.PutToken(token)
}
log.Printf("Got token: %#v", t)
log.Printf("Got token: %#v", token)
_, _ = machType, imageURL
}