1

I am trying to implement the verification token id from server side using Firebase Admin SDK(java), but i am getting the error:

Failed to parse service account: 'project_id' must be set

I generated my credentials in the following path from firebase:

Project settings->Service Accounts and generate a new private key and I have this:

    {
        "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...."
}

Also the way that I load the JSON file it`s the same as the firebase web page:

for maven:

enter image description here

to load my json file using java:

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

and the last part when i am trying to verify the token 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();

            }
        });

I think that i did the same that the firebase web page mentions but i am getting the above error

Can somebody help me?

I would appreciate your help.

Pierre.Vriens
  • 2,117
  • 75
  • 29
  • 42
  • What version of the Admin Java SDK are you using? Can you repro this on the latest 4.1.5 version? – jwngr Apr 01 '17 at 00:54
  • Also, are you sure the `"path/to/serviceAccountKey.json"` path is actually the correct path to your JSON file? It's possible you are referencing a file which doesn't exist. – jwngr Apr 01 '17 at 00:55
  • Hello, yes I'am using the lastest version of firebase admin 4.1.5 and yes The path of my file corresponds to my path of java, so I only specify the name of my file, In fact after loading the file, I print it in console and it is like the json file. – Luis Perez Galindo Apr 02 '17 at 04:55
  • To load the file right now I am doing this, with the same – Luis Perez Galindo Apr 02 '17 at 05:04
  • InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("/apilogintest-9c5f5-firebase-adminsdk-hhayl-4c4cbbe5f2.json"); BufferedReader in = new BufferedReader(new InputStreamReader(serviceAccount)); String line = null; while((line = in.readLine()) != null){ System.out.println(line); } FirebaseOptions options = new FirebaseOptions.Builder() .setCredential(FirebaseCredentials.fromCertificate(serviceAccount)) .setDatabaseUrl("https://apilogintest-9c5f5.firebaseio.com").build(); initializeApp = FirebaseApp.initializeApp(options); – Luis Perez Galindo Apr 02 '17 at 05:04
  • I think the SDK is just having an issue parsing the service account JSON you are passing in. It must be because the JSON is invalid somehow. Can you try running the following code and seeing if there is a useful error: `try { jsonData = streamToString(serviceAccount); JSONObject jsonObject = new JSONObject(jsonData); projectId = jsonObject.getString("project_id"); } catch (JSONException e) { throw new RuntimeException(e); }` – jwngr Apr 02 '17 at 18:28

1 Answers1

0

I figured out the issue.

The problem was when i tried to revalidate the JSON object, printing the JSON object in console, I don't know why but i can't perform any proccess with the Stream(can't use BufferedReader also) before setting the firebase options, i mean, the following code doesn`t run, showing us the error:

Firebase: Failed to parse service account: 'project_id' must be set

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

but if I load the file directly as follows without any block code before(no bufferedReader or jsonData = streamToString(serviceAccount);....):

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

I don`t know why this behavior but now is working.

thank you for your support!

Thomas David Kehoe
  • 10,040
  • 14
  • 61
  • 100