名称为[DEFAULT]的FirebaseApp不存在; Firebase令牌验证

时间:2016-07-28 20:13:29

标签: java maven authentication token firebase-authentication

为了验证从客户端应用程序发送的Firebase令牌,我有以下代码:

Task<FirebaseToken> authTask = FirebaseAuth.getInstance().verifyIdToken("very long string token").addOnSuccessListener(new OnSuccessListener() {
@Override
public void onSuccess(Object tr) { }
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception excptn) { }
}).addOnCompleteListener(new OnCompleteListener() {
@Override
public void onComplete(Task task) { }
});
try {
Tasks.await(authTask);
} catch(ExecutionException | InterruptedException e ){
//handle error
}
FirebaseToken decodedToken = authTask.getResult();
String uid = decodedToken.getUid();

通过Maven / google后端api部署时,我收到以下错误:

java.lang.IllegalStateException:名称为[DEFAULT]的FirebaseApp不存在

我该如何解决?

1 个答案:

答案 0 :(得分:2)

  1. 您必须在Google云端控制台上设置服务帐户。阅读Docs

  2. 将服务帐户密钥文件(.json)放入/resources/META-INF/目录。

  3. 确保您已将资源文件包含在appengine-web.xml

  4.    
    <!-- Include resource files -->
    <resource-files>
        <include path="/**.json" />
        <include path="/**.xml" />
        <include path="/**.properties" />
    </resource-files>
    
    1. 使用生成的FirebaseOptions文件传递service_account_key.json以初始化FirebaseApp。您可以在Warmup Servlet
    2. 中执行此操作
      public void init(ServletConfig config) throws ServletException {
      
          InputStream loadedServiceAccount = config
              .getServletContext()
              .getResourceAsStream("/WEB-INF/service_account_key.json");
      
          FirebaseOptions options = new FirebaseOptions.Builder()
              .setServiceAccount(loadedServiceAccount)
              .setDatabaseUrl("URL_FIREBASE_DATABASE").build();
      
          FirebaseApp firebaseApp = FirebaseApp.initializeApp(options);
          FirebaseAuth.getInstance(firebaseApp);
      
      }
      
      1. 然后,您可以继续在代码中使用FirebaseAuth.getInstance()
      2. 当您的AppEngine实例运行测试版(可能是1.9.44)时,它将为您提供noMethodFoundError。然后你可以继续这个Question