Manza
Manza

Reputation: 3527

Firebase Admin Java SDK not doing any operation

I've setup the firebase admin SDK using this guide

So I'm initializing it in the following way.

InputStream refreshToken = new ClassPathResource(CONFIG_FILE).getInputStream();

FirebaseOptions options = new FirebaseOptions.Builder()
            .setCredential(FirebaseCredentials.fromRefreshToken(refreshToken))
            .setDatabaseUrl("https://<project-id>.firebaseio.com/")
            .build();

FirebaseApp.initializeApp(options);

I got no errors to this point, everything seems fine. However, I'm unable to do any operations. Calling the methods does nothing. Eg:

FirebaseDatabase.getInstance()
            .getReference()
            .addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                      //this callback is never called
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

or

class Pojo {

    public Pojo(String name, String surname) {
        this.name = name;
        this.surname = surname;
    }

    String name;
    String surname;
    //omitted getters and setters
}


FirebaseDatabase.getInstance.getReference()
                .push()
                .setValue(new Pojo("Test", "Test"));
//this value is never set

It simply looks like that the skd ignores any command. Any ideas?

Background: I'm running this as a web application (spring) on a local machine.

Upvotes: 2

Views: 472

Answers (1)

jwngr
jwngr

Reputation: 4422

If you are authenticating the SDK with a service account key JSON file as documented in Add Firebase to your app, you should use the fromCertificate() method, not the fromRefreshToken() method:

FileInputStream serviceAccount = new FileInputStream("path/to/serviceAccountKey.json");

FirebaseOptions options = new FirebaseOptions.Builder()
  .setCredential(FirebaseCredentials.fromCertificate(serviceAccount))
  .setDatabaseUrl("https://<DATABASE_NAME>.firebaseio.com/")
  .build();

FirebaseApp.initializeApp(options);

Authenticating with a refresh token is a bit of an advanced use case and is probably not what you want to be using.

Upvotes: 2

Related Questions