Firebase:无法解析服务帐户:必须设置'project_id'

时间:2017-03-31 20:02:25

标签: java firebase firebase-authentication firebase-admin

我正在尝试使用Firebase Admin SDK(java)从服务器端实现验证令牌ID,但我收到错误:

无法解析服务帐户:必须设置'project_id'

我在firebase的以下路径中生成了我的凭据:

项目设置 - >服务帐户并生成一个新的私钥,我有:

    {
        "type": "service_account",
        "project_id": "apilogintest-9c5f5",
        "private_key_id": "<privateKeyId...>",
        "private_key": "-----BEGIN PRIVATE KEY-----\n<a really big private Key>\n-----END PRIVATE KEY-----\n",
        "client_email": "<clientEmail>",
        "client_id": "<clientId>",
        "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/firebase-adminsdk-hhay...."
}

此外,我加载JSON文件的方式与firebase网页相同:

对于maven:

enter image description here

使用java加载我的json文件:

    FileInputStream serviceAccount = new FileInputStream("path/to/serviceAccountKey.json");
    FirebaseOptions options = new FirebaseOptions.Builder()
      .setCredential(FirebaseCredentials.fromCertificate(serviceAccount))
      .setDatabaseUrl("https://apilogintest-9c5f5.firebaseio.com/")
      .build();
   FirebaseApp initializeApp = FirebaseApp.initializeApp(options);

以及我尝试验证令牌ID时的最后一部分:

FirebaseAuth.getInstance(initializeApp).verifyIdToken(token)
        .addOnSuccessListener(new OnSuccessListener<FirebaseToken>() {
            @Override
            public void onSuccess(FirebaseToken decodedToken) {
               String uid = decodedToken.getUid();
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                e.printStackTrace();

            }
        });

我认为我做的与firebase网页提到的相同,但我收到了上述错误

有人能帮助我吗?

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我想出了这个问题。

问题是当我尝试重新验证JSON对象,在控制台中打印JSON对象时,我没有t know why but i can在设置firebase之前使用Stream执行任何进程(也不能使用BufferedReader)选项,我的意思是,以下代码没有运行,向我们显示错误:

Firebase:无法解析服务帐户:&#39; project_id&#39;必须设置

try {
        String jsonData = streamToString(serviceAccount);
        JSONObject jsonObject = new JSONObject(jsonData);
        String projectId = jsonObject.getString("project_id");
        System.out.println(projectId);
    } catch (JSONException e) {
        throw new RuntimeException(e);
    }

FirebaseOptions options = new FirebaseOptions.Builder()
            .setCredential(FirebaseCredentials.fromCertificate(serviceAccount))
            .setDatabaseUrl("https://apilogintest-9c5f5.firebaseio.com").build();
    initializeApp = FirebaseApp.initializeApp(options);

但如果我之前没有任何块代码直接加载文件(没有bufferedReader或jsonData = streamToString(serviceAccount); ....):

   FirebaseOptions options = new FirebaseOptions.Builder()
                .setCredential(FirebaseCredentials.fromCertificate(fl))
                .setDatabaseUrl("https://apilogintest-9c5f5.firebaseio.com").build();
        initializeApp = FirebaseApp.initializeApp(options);

我不知道为什么这种行为现在正在发挥作用。

感谢您的支持!