【问题标题】:Firebase: Failed to parse service account: 'project_id' must be setFirebase:无法解析服务帐户:必须设置“project_id”
【发布时间】:2017-08-26 04:55:29
【问题描述】:

我正在尝试使用 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:

使用 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 网页提到的相同的操作,但我收到了上述错误

有人可以帮帮我吗?

感谢您的帮助。

【问题讨论】:

  • 您使用的是哪个版本的 Admin Java SDK?你能在最新的 4.1.5 版本上重现这个吗?
  • 另外,您确定 "path/to/serviceAccountKey.json" 路径实际上是您的 JSON 文件的正确路径吗?您可能正在引用一个不存在的文件。
  • 你好,是的,我使用的是最新版本的firebase admin 4.1.5,是的,我的文件路径对应我的java路径,所以我只指定我的文件名,其实加载文件后,我在控制台打印它,它就像 json 文件。
  • 现在要加载文件,我正在这样做,同样
  • InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("/apilogintest-9c5f5-firebase-adminsdk-hhayl-4c4cbbe5f2.json"); BufferedReader in = new BufferedReader(new InputStreamReader(serviceAccount));字符串线=空; while((line = in.readLine()) != null){ System.out.println(line); } FirebaseOptions options = new FirebaseOptions.Builder() .setCredential(FirebaseCredentials.fromCertificate(serviceAccount)) .setDatabaseUrl("apilogintest-9c5f5.firebaseio.com").build(); initializeApp = FirebaseApp.initializeApp(options);

标签: java firebase firebase-authentication firebase-admin


【解决方案1】:

我发现了问题。

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

Firebase:无法解析服务帐号:必须设置“project_id”

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

我不知道为什么会出现这种行为,但现在可以了。

感谢您的支持!

【讨论】:

    猜你喜欢
    • 2023-03-08
    • 1970-01-01
    • 2021-10-19
    • 2018-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多