2013-03-13 3 views

ответ

7

Использование https://code.google.com/p/goauth2/

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

Получите вашу электронную почту службы и ваш p12 закрытый ключ из Google API Console. На данный момент он не может читать файлы P12 так раздеть их только ключом RSA с OpenSSL 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) 
    } 
Смежные вопросы