2

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.

Manza
  • 3,427
  • 4
  • 36
  • 57
  • Two comments / questions: (1) where are you getting the refresh token and are you sure it has proper permission to your project (using refresh tokens is an advanced use case) and (2) the Admin SDKs are only meant for *server* applications and are not secure to use on client applications. – jwngr Feb 14 '17 at 15:58
  • 1. I'm using the credential json file, followed the tutorial from google docs. 2. This is a server application, not client – Manza Feb 14 '17 at 21:50

1 Answers1

2

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.

jwngr
  • 4,284
  • 1
  • 24
  • 27