3

Go で JWT を使用してサービス アカウントを承認するにはどうすればよいですか?

4

1 に答える 1

7

https://code.google.com/p/goauth2/を使用

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

Google 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)
  }
于 2013-03-13T00:04:59.577 に答える