3
如何在Go中授權JWT的服務帳戶?帶有JWT的Google服務帳戶授權
如何在Go中授權JWT的服務帳戶?帶有JWT的Google服務帳戶授權
使用https://code.google.com/p/goauth2/
go get code.google.com/p/goauth2/oauth
獲取你的服務的電子郵件,並從谷歌API控制檯您的P12私鑰。現在它無法讀取P12文件,從而剝奪他們只是使用OpenSSL openssl pkcs12 -in file.p12 -nocerts -out key.pem -nodes
RSA密鑰,然後刪除多餘的文字
然後:
package main
import (
"code.google.com/p/goauth2/oauth/jwt"
"flag"
"fmt"
"http"
"io/ioutil"
)
var (
serviceEmail = flag.String("service_email", "", "OAuth service email.")
keyPath = flag.String("key_path", "key.pem", "Path to unencrypted RSA private key file.")
scope = flag.String("scope", "", "Space separated scopes.")
)
func fetchToken() (string, error) {
// Read the pem file bytes for the private key.
keyBytes, err := ioutil.ReadFile(*keyPath)
if err != nil {
return "", err
}
t := jwt.NewToken(*serviceEmail, *scope, keyBytes)
c := &http.Client{}
// Get the access token.
o, err := t.Assert(c)
if err != nil {
return "", err
}
return o.AccessToken, nil
}
func main() {
flag.Parse()
token, err := fetchToken()
if err != nil {
fmt.Printf("ERROR: %v\n", err)
} else {
fmt.Printf("SUCCESS: %v\n", token)
}