【问题标题】:Path to Firebase Admin Key in Java servletJava servlet 中 Firebase 管理员密钥的路径
【发布时间】:2017-05-06 07:58:23
【问题描述】:

我一直收到一个

FileNotFoundException

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

这是一个特定错误,因为我试图在 Android 模块 Servlet 中初始化 Firebase。

这是我的模块项目结构:

这是我的 Serlvet 中尝试初始化 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,但是有人如何能够正确引用您的包结构中的文件?非常感谢任何帮助。

提前致谢!

【问题讨论】:

    标签: java android servlets firebase firebase-realtime-database


    【解决方案1】:

    我已经这样解决了这个问题:

    我需要将 .json 文件放在我的 WEB-INF 文件夹中,然后用户 ServletContext 以流的形式获取资源,这是我的示例:

            FirebaseOptions options = null;
            ServletContext ctx = getServletContext();
            InputStream in = ctx.getResourceAsStream("/WEB-INF/tt-social-firebase-adminsdk.json");
            try {
                options = new FirebaseOptions.Builder()
                        .setServiceAccount(in)
                        .setDatabaseUrl(FIREBASE_DATABASE_URL)
                        .build();
            } catch (Exception e) {
                e.printStackTrace();
            }
    

    这解决了我在访问文件时遇到的所有问题。希望这对其他人有帮助。

    【讨论】:

      【解决方案2】:

      当我在How to Use Facebook Account Kit to Authenticate Firebase App Users on Android 偶然发现这篇文章时,我已经想尽办法在系统中读取 JSON 密钥文件。在本文中,它详细解释了如何在 Cloud Endpoints 模块中设置 Firebase Admin SDK。我在这里总结一下步骤:

      1. 将您的 serviceAccountKey.json 放在应用的 WEB-INF 文件夹中。
      2. 在你的 appengineweb.xml 中插入这个:

        <resource-files>
            <include path="/**.json" />
        </resource-files>
        
      3. 在下面的某个地方定义一个类。强制使用单独的 init() 方法:

        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) {}
        }
        
      4. 在您定义的任何 Endpoints 中的任何 static{} 代码中调用此方法。例如:

        static {
            ObjectifyService.register(FCMTokenMap.class);
            FirebaseService.init();
        }
        
        1. 您可以从任何地方调用涉及数据库、FCM 等的其他 Firebase 方法......!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-02-14
        • 1970-01-01
        • 2019-12-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多