2016-09-20 5 views
0

간단한 로깅 어플리케이션에 새로운 Firebase Realtime Database를 사용하려고합니다. 데이터베이스와의 모든 간섭은 내 서버에서 발생하므로 읽기/쓰기가 가능한 계정이 하나만 있으면됩니다.이동 중에 Firebase Realtime Database에 대한 서비스 계정 인증

내가 알 수있는 한, 문서는 끔찍합니다. 많은 내용이 있지만 모순이 있으며, 절반은 '오래된'Firebase를위한 것이며, 자주 사용하지 않는 임의의 언어입니다), 서비스 계정을 만든 다음 OAuth를 사용하여 JWT 토큰을 만들어야합니다. 다행히도 Go에는 라이브러리가 내장되어 있습니다. 나는 그것이 오류가 invalid_scope이다 일종으로 가정하고 설명이 말한다

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

:

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

문제는, 난 항상이 오류가 : 여기 내 코드입니다 유효하지 않음 audience (JWT aud 매개 변수로 가정).

기본 범위 "auth != null" 규칙을 사용하여 Firebase 데이터베이스를 읽고 쓸 수있는 범위는 무엇입니까?

편집 : 실제로 게시물을 할 때 지금은 나에게 403 응답을 제공하지만

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

: 나는 마침내 대답 here을 발견했다.

{ 
    "error" : "Permission denied." 
} 

답변

1

윽, 난 문서화되지 않은 대답 here을 발견했다. 현재이 범위가 필요합니다.

https://www.googleapis.com/auth/userinfo.email 
관련 문제