Created
May 19, 2016 17:02
-
-
Save matthewbelisle-wf/dd14390b2293e4895a99f0409f36ba6f to your computer and use it in GitHub Desktop.
Pub credentials.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"flag" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"os" | |
"golang.org/x/oauth2/google" | |
) | |
const usagePrefix = `Makes a pub credentials.json file given a service account private key | |
Usage: go run ./main.go [OPTIONS] | |
OPTIONS: | |
` | |
var ( | |
keyFlag = flag.String("key", "service_account_key.json", "Private key json file") | |
) | |
func main() { | |
// Flag setup | |
flag.Usage = func() { | |
fmt.Fprint(os.Stderr, usagePrefix) | |
flag.PrintDefaults() | |
} | |
flag.Parse() | |
// Get token info | |
jsonKey, err := ioutil.ReadFile(*keyFlag) | |
if err != nil { | |
log.Fatalf("Could not read %s: %v", *keyFlag, err) | |
} | |
tokenSource, err := google.JWTAccessTokenSourceFromJSON([]byte(jsonKey), "") | |
if err != nil { | |
log.Fatalf("Error getting token source: %v", err) | |
} | |
token, err := tokenSource.Token() | |
if err != nil { | |
log.Fatalf("Error getting token: %v", err) | |
} | |
json.NewEncoder(os.Stdout).Encode(map[string]interface{}{ | |
"accessToken": token.AccessToken, | |
"refreshToken": token.RefreshToken, | |
"tokenEndpoint": google.Endpoint.TokenURL, | |
"expiration": token.Expiry.UnixNano() / 1000000, // Milliseconds | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment