问题
I am trying to integrate my spring-boot rest api with Firebase by these following tutorial: savicprvoslav
I need to verify the userTokenId using Firebase SDK which is generated by a front end application.
The above git guide used
<dependency>
<groupId>com.google.firebase</groupId>
<artifactId>firebase-server-sdk</artifactId>
<version>3.0.3</version>
</dependency>
which is already deprecated and suggesting to use latest Firebase SDK [ maven repository]
According to the Firebase SDK integration guide we should use
<dependency>
<groupId>com.google.firebase</groupId>
<artifactId>firebase-admin</artifactId>
<version>5.4.0</version>
</dependency>
Old practice:
InputStream inputStream = FirebaseConfig.class.getClassLoader().getResourceAsStream(configPath);
FirebaseOptions options = new FirebaseOptions.Builder().setServiceAccount(inputStream)
.setDatabaseUrl(databaseUrl).build();
New Practice:
FileInputStream serviceAccount = new FileInputStream("path/to/serviceAccountKey.json");
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.setDatabaseUrl("https://<DATABASE_NAME>.firebaseio.com/")
.build();
I have tried to use it (removed the old one) but it failed to resolve lots of dependencies like FirebaseOption which can only resolved by old maven dependencies(firebase-server-sdk 3.0.3)
I am really very confused ! Can not find any updated example project. A sample project will very helpful (example code which initialize the firebase sdk ) .
Can anybody help?
Thanks in advnace
回答1:
Fortunately Firebase has updated their documentation and now suggesting these below maven configuraiton , which resolved the problem perfectly. I think it was a outdated documentation issue.
<dependency>
<groupId>com.google.firebase</groupId>
<artifactId>firebase-admin</artifactId>
<version>5.5.0</version>
</dependency>
Please follow this link Firebase SDK Installation guide
In addition , if you are behind corporate proxy, you need to set proxy like this:
System.setProperty("https.proxyHost", "your_proxy_host");
System.setProperty("https.proxyPort", "your_proxy_port");
so complete code to initialize Firebase instance is:
URL fileUrl = FirebaseConfig.class.getClassLoader().getResource(configPath);
FileInputStream fisTargetFile = new FileInputStream(fileUrl.getFile());
System.setProperty("https.proxyHost", "your_proxy_host");
System.setProperty("https.proxyPort", "your_proxy_port");
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(fisTargetFile))
.setDatabaseUrl(databaseUrl)
.build();
FirebaseApp.initializeApp(options);
Hope it will be helpful to others.
来源:https://stackoverflow.com/questions/47156777/can-not-resolve-firebaseoptions-using-latest-firebase-sdk-dependency