0
votes

I am using this guide to initialize the firebase admin sdk on my spring-boot java application: https://firebase.google.com/docs/admin/setup

I've included the correct maven dependency

<dependency>
    <groupId>com.google.firebase</groupId>
    <artifactId>firebase-admin</artifactId>
    <version>5.9.0</version>
</dependency>

And I'm attempting to initialize using the code snippet they provided, but when I import the firebase libraries, none of the symbols (firebase, auth, FirebaseOptions, GoogleCredentials, FirebaseApp) are being resolved.

import java.io.FileInputStream;
import com.google.firebase.*;
import com.google.auth.oauth2.GoogleCredentials;

import org.springframework.context.annotation.Configuration;

@Configuration
public class FirebaseAdminConfig {
    FileInputStream serviceAccount = new 
FileInputStream("path/to/firebase/credentials/");
    FirebaseOptions options = new FirebaseOptions.Builder()
            .setCredentials(GoogleCredentials.fromStream(serviceAccount))
            .setDatabaseUrl("link to database")
            .build();
FirebaseApp.initializeApp(options);
}

Am I missing import statements? Is there some additional configuration needed?

2
Have you update your new imported dependency? - Blackjack

2 Answers

2
votes

In your FirebaseAdminConfig class, you have directly pasted the snippet. You need to create a method first, otherwise, you will get compilation errors.

@Configuration
public class FireBaseConfig{
@Bean
    FirebaseApp createFireBaseApp() throws IOException {
    FileInputStream serviceAccount =
            new FileInputStream("pathtojson.json");

    FirebaseOptions options = new FirebaseOptions.Builder()
            .setCredentials(GoogleCredentials.fromStream(serviceAccount))
            .setDatabaseUrl("url")
            .build();

  return  FirebaseApp.initializeApp(options);
}

}
0
votes

You can also do it in main class of Application

public static void main(String[] args) throws IOException {
    FileInputStream serviceAccount = new FileInputStream("src/main/resources/credential.json");
    @SuppressWarnings("deprecation")
    FirebaseOptions options = new FirebaseOptions.Builder()
            .setCredentials(GoogleCredentials.fromStream(serviceAccount))
            .build();
    FirebaseApp.initializeApp(options);
    SpringApplication.run(MarketingDashboardApplication.class, args);
}