Java servlet中Firebase管理员密钥的路径 [英] Path to Firebase Admin Key in Java servlet

查看:88
本文介绍了Java servlet中Firebase管理员密钥的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不断收到

FileNotFoundException

FileNotFoundException

当尝试使用 Firebase 初始化我的应用时说找不到我的adminsdk.json服务密钥.

when trying to initialize my app with Firebase saying it cannot find my adminsdk.json service key.

这是一个特定的错误,因为我正在尝试在 Android模块Servlet 中初始化Firebase.

This is a specific error as I am trying to initialize Firebase in an Android Module Servlet.

这是我的模块项目结构:

Here is what my module project structure looks like:

这是我的Serlvet中试图初始化FirebaseOptions的代码:

And here is the code in my Serlvet that is trying to intialize FirebaseOptions:

FirebaseOptions options = null;
        try {
            options = new FirebaseOptions.Builder()
                    .setServiceAccount(new FileInputStream("leadsbackend\\src\\main\\webapp\\tt-social-firebase-adminsdk.json"))
                    .setDatabaseUrl(FIREBASE_DATABASE_URL)
                    .build();
        } catch (Exception e) {
            e.printStackTrace();
        }

这是控制台中的错误:

java.io.FileNotFoundException: TTLeads\leadsbackend\src\main\webapp\tt-social-firebase-adminsdk.json (The system cannot find the path specified)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:195)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at java.io.FileInputStream.<init>(FileInputStream.java:93)
    at com.nicholas.leadsbackend.LeadsServlet.sendToFirebase(LeadsServlet.java:63)
    at com.nicholas.leadsbackend.LeadsServlet.sendToBase(LeadsServlet.java:57)
    at com.nicholas.leadsbackend.LeadsServlet.doGet(LeadsServlet.java:37)

我很确定我没有正确设置URI,但是有人将如何正确引用包结构中的文件?任何帮助都非常感谢.

I am pretty sure I am not setting the URI correctly, but how would someone be able to reference a file in your package structure correctly? Any help is very appreciated.

提前谢谢!

推荐答案

当我在以下位置偶然发现这篇文章时,我已经绞尽脑汁,无法读取系统中的JSON密钥文件: atimothee.xyz/blog/2016/09/17/how-to-use-facebook-account-kit-to-authenticate-firebase-app-users-on-android/http://atimothee.xyz/blog/2016/09/17/how-to-use-facebook-account-kit-to-authenticate-firebase-app-users-on-android/"rel =" nofollow noreferrer>如何使用Facebook帐户工具包在以下位置对Firebase应用程序用户进行身份验证Android .在本文中,它详细说明了如何在Cloud Endpoints模块中设置Firebase Admin SDK.我将在这里总结步骤:

I had cracked my head to enable reading of JSON key file in the system, when I stumbled on this article at: How to Use Facebook Account Kit to Authenticate Firebase App Users on Android. In this article, it explains in details to how to setup Firebase Admin SDK in a Cloud Endpoints module. I will summarize the steps here:

  1. serviceAccountKey.json 放置在应用程序的WEB-INF文件夹中.
  2. 将此内容插入您的appengineweb.xml:

  1. Place your serviceAccountKey.json in your apps' WEB-INF folder.
  2. Insert this in your appengineweb.xml:

<resource-files>
    <include path="/**.json" />
</resource-files>

  • 定义一个类,如下所示.强制使用单独的init()方法:

  • Define a class somewhere like the one below. Have a seperate init() method compulsorily:

    public class FirebaseService {
    
    public static void init() {
        try {
            FileInputStream serviceAccount = new FileInputStream(new File("WEB-INF/serviceAccountKey.json"));
            FirebaseOptions options = new FirebaseOptions.Builder()
                .setCredential(FirebaseCredentials.fromCertificate(serviceAccount))
                .setDatabaseUrl("https://[YOUR_APP_NAME].firebaseio.com/")
                .build();
            FirebaseApp.initializeApp(options);
            System.out.print("In Firebase Init module...!!");
        } catch(FileNotFoundException ignored) {}
    }
    

  • 在您定义的任何端点中的任何 static {} 代码中调用此方法.例如:

  • Invoke this method in any of the static{} code in any of the Endpoints defined by you. For example:

    static {
        ObjectifyService.register(FCMTokenMap.class);
        FirebaseService.init();
    }
    

    1. 您可以从任何地方调用涉及数据库,FCM等的其他Firebase方法.
    1. You can invoke other Firebase methods which involve Database, FCM, etc from anywhere..!

  • 这篇关于Java servlet中Firebase管理员密钥的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆