Firebase Admin Java SDK not doing any operation

匿名 (未验证) 提交于 2019-12-03 09:05:37

问题:

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.

回答1:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!