问题
I keep getting a
FileNotFoundException
when trying to initialize my app with Firebase saying it cannot find my adminsdk.json service key.
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:
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();
}
And here is the error in the console:
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)
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.
Thanks in advance!
回答1:
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:
- Place your serviceAccountKey.json in your apps' WEB-INF folder.
Insert this in your appengineweb.xml:
<resource-files> <include path="/**.json" /> </resource-files>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) {} }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(); }- You can invoke other Firebase methods which involve Database, FCM, etc from anywhere..!
回答2:
I have resolved this issue like so:
I needed to place the .json file in my WEB-INF folder, and then user ServletContext to grab the resource as a stream, here is my example:
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();
}
That resolved all issues I was having accessing the file. Hope this helps someone else.
来源:https://stackoverflow.com/questions/41253548/path-to-firebase-admin-key-in-java-servlet