Firebase Admin SDK 5.0.0,setCredentials无法解析方法** Java **

时间:2017-05-30 13:10:57

标签: android google-app-engine firebase firebase-realtime-database

这是使用Android Studio 2.3.2在Servlet模块上运行

private FirebaseApp app = null; // To avoid NullPointerExeception
private FirebaseDatabase database = FirebaseDatabase.getInstance(); // the error points here: FirebaseApp with name [DEFAULT] doesn't exist.
private DatabaseReference myRef = database.getReference("table/ID"); //Table from Firebase

这是在Get方法中,我只想读取一个值(保持简单)。 我的问题是在setCredential上它只是说“无法解决方法”

我进入了后端gradle文件:

appengineSdk 'com.google.appengine:appengine-java-sdk:1.9.34'
compile 'com.google.appengine:appengine-api-1.0-sdk:1.9.53'
compile 'org.apache.httpcomponents:httpclient:4.5.3'
compile 'javax.servlet:servlet-api:2.5'
compile 'com.google.firebase:firebase-server-sdk:3.0.3'
compile 'com.google.firebase:firebase-admin:5.0.0'

我尝试了几种不同的方法来实现凭据,因为您可以看到它们是“//评论”

只有一个正在使用Admin SDK 4+“setServiceAccount” nowdays已弃用,如果我将其编译并部署到App Engine,然后Firebase想要读取它所说的表: java.lang .IllegalStateException:名称为[DEFAULT]的FirebaseApp不存在。

所以我在这里有两个选择(我愿意听取你的一切):

  1. setCredentials不起作用
  2. setServiceAccount编译但不起作用

    InputStream serviceAccount = this.getClass().getResourceAsStream("/WEB-INF/YourJsonFile.json"); //JsonFile from Account Services 
    FirebaseOptions options = new FirebaseOptions.Builder()
        .setCredential(FirebaseCredentials.fromCertificate(serviceAccount)) // example (doesn't compile)
      //.setCredential(FirebaseCredentials.applicationDefault()) //same problem (doesn't compile)
      //.setServiceAccount(serviceAccount)                    //compiling & it throws the error: FirebaseApp with name [DEFAULT] doesn't exist.
        .setDatabaseUrl("https://YourDatabaseName.firebaseio.com")
        .build();
    this.app = FirebaseApp.initializeApp(options);
    
  3. 我正在调用 myRef 来读取这样的值:

         myRef.addListenerForSingleValueEvent(new ValueEventListener() {
    

    当我运行应用程序时,它指向FirebaseDatabase.getInstance 行(在属性上)

    由于

0 个答案:

没有答案