Go 谷歌服务帐户授权与JWT在围棋

Go 谷歌服务帐户授权与JWT在围棋,go,Go,如何授权JWT在Go中的服务帐户?使用 go-get-code.google.com/p/goauth2/oauth 从谷歌API控制台获取您的服务电子邮件和p12私钥。目前,它无法读取p12文件,因此使用openssl将它们剥离为rsa密钥 openssl pkcs12-in file.p12-nocerts-out key.pem-nodes然后删除额外的文本 然后: package main import ( "code.google.com/p/goauth2/oauth/jwt"

如何授权JWT在Go中的服务帐户?

使用

go-get-code.google.com/p/goauth2/oauth

从谷歌API控制台获取您的服务电子邮件和p12私钥。目前,它无法读取p12文件,因此使用openssl将它们剥离为rsa密钥
openssl pkcs12-in file.p12-nocerts-out key.pem-nodes
然后删除额外的文本

然后:

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)
  }

请注意,code.google.com/p/goauth2现在已被弃用——它已被golang.org/x/oauth2取代