Created
September 19, 2019 20:15
-
-
Save dhulihan/1edc53fc37ea069055f4b499c01dc698 to your computer and use it in GitHub Desktop.
Convert Google Service Account Private Key (stdin) to JWT Token
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 | |
// | |
// this converts a google service account key to a jwt | |
// | |
import ( | |
"encoding/json" | |
"flag" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"os" | |
"golang.org/x/oauth2/google" | |
) | |
func main() { | |
b, err := ioutil.ReadAll(os.Stdin) | |
if err != nil { | |
log.Fatal(err) | |
} | |
var audience = flag.String("audience", "", "audience") | |
// generate token | |
source, err := google.JWTAccessTokenSourceFromJSON(b, *audience) | |
if err != nil { | |
log.Fatal(err) | |
} | |
token, err := source.Token() | |
if err != nil { | |
log.Fatal(err) | |
} | |
json, err := json.Marshal(token) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Printf("%s", json) | |
} |
Author
dhulihan
commented
Sep 19, 2019
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment