2016-09-20 6 views
0

私は、単純なロギングアプリケーションのために新しいFirebaseリアルタイムデータベースを使用しようとしています。データベースとの干渉は私のサーバーからのものなので、何かを読み書きできるアカウントが1つだけ必要です。GoでのFirebase Realtime Databaseのサービスアカウントの認証

私が知る限りでは(ドキュメンテーションはひどいですが、それはたくさんありますが、それは矛盾し、その半分は '古い' Firebase用であり、しばしばあなたが使用していないランダムな言語です)、私はサービスアカウントを作成し、OAuthを使用してJWTトークンを作成する必要があります。幸いなことに、Goにはこれに適したライブラリがあります。ここに私のコードは次のとおりです。

const firebasePostUrl = "https://my-product-logging.firebaseio.com/tests.json" 

// Obtained from the Google Cloud API Console 
var firebaseServiceAccount map[string]string = map[string]string{ 
    "type":      "service_account", 
    "project_id":     "my-product-logging", 
    "private_key_id":    "1c35ac0c501617b8f1610113c492a5d3321f4318", 
    "private_key":     "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoblahblahhwWlteuRDrsxmRq+8\ncDGMKcXyDHl3nWdIrWqJcDw=\n-----END PRIVATE KEY-----\n", 
    "client_email":    "[email protected]", 
    "client_id":     "101403085113430683797", 
    "auth_uri":     "https://accounts.google.com/o/oauth2/auth", 
    "token_uri":     "https://accounts.google.com/o/oauth2/token", 
    "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", 
    "client_x509_cert_url":  "https://www.googleapis.com/robot/v1/metadata/x509/log-user%40my-product-logging.iam.gserviceaccount.com", 
} 

func firebaseClient() *http.Client { 
    jwtConfig := jwt.Config{ 
     // Email is the OAuth client identifier used when communicating with 
     // the configured OAuth provider. 
     Email: firebaseServiceAccount["client_email"], 

     // PrivateKey contains the contents of an RSA private key or the 
     // contents of a PEM file that contains a private key. The provided 
     // private key is used to sign JWT payloads. 
     // PEM containers with a passphrase are not supported. 
     // Use the following command to convert a PKCS 12 file into a PEM. 
     // 
     // $ openssl pkcs12 -in key.p12 -out key.pem -nodes 
     // 
     PrivateKey: []byte(firebaseServiceAccount["private_key"]), 

     // PrivateKeyID contains an optional hint indicating which key is being 
     // used. 
     PrivateKeyID: firebaseServiceAccount["private_key_id"], 

     // Subject is the optional user to impersonate. 
     Subject: "", 

     // Scopes optionally specifies a list of requested permission scopes. 
     Scopes: []string{ 
      "https://www.googleapis.com/auth/devstorage.readonly", 
     }, 

     // TokenURL is the endpoint required to complete the 2-legged JWT flow. 
     TokenURL: firebaseServiceAccount["token_uri"], 

     // Expires optionally specifies how long the token is valid for. 
     Expires: 0, 
    } 

    ctx := context.Background() 
    return jwtConfig.Client(ctx) 
} 

func firebaseFunc() { 

    authedClient := firebaseClient() 

    msg := map[string]string{ 
     "hello": "there", 
     "every": "one", 
    } 

    data, err := json.Marshal(msg) 
    if err != nil { 
     log.Fatal("JSON Marshall Error: ", err) 
     continue 
    } 

    resp, err := authedClient.Post(firebasePostUrl, "application/json", bytes.NewReader(data)) 
    if err != nil { 
     log.Fatal("Firebase Error: ", err) 
     continue 
    } 

    log.Print("Firebase Response Code: ", resp.StatusCode) 
} 

問題は、私はいつもこのエラーを取得するには、次のとおりです。

{ 
    "error" : "invalid_scope", 
    "error_description" : "https://www.googleapis.com/auth/devstorage.readonly is not a valid audience string." 
} 

私はそれがエラーがinvalid_scopeであるため、説明は、それがあると言うタイプであると仮定します無効audience(私はJWT audパラメータと仮定します)。

ファイアベースデータベースを読み書きできるようにするにはどうすればよいですか(デフォルトの"auth != null"ルールを使用します)。

編集:実際にポストをやったときに、今では私に403応答を与えるしかし

https://www.googleapis.com/auth/firebase 

:私は最終的に答えhereを見つけました。

{ 
    "error" : "Permission denied." 
} 

答えて

1

うわ、私は文書化されていない答えhereを見つけました。現在、この範囲も必要です。

https://www.googleapis.com/auth/userinfo.email 
関連する問題