我正在使用本指南在我的spring-boot java应用程序上初始化firebase admin sdk:https://firebase.google.com/docs/admin/setup
我已经包含了正确的maven依赖
<dependency>
<groupId>com.google.firebase</groupId>
<artifactId>firebase-admin</artifactId>
<version>5.9.0</version>
</dependency>
我尝试使用他们提供的代码段进行初始化,但是当我导入firebase库时,没有任何符号(firebase,auth,FirebaseOptions,GoogleCredentials,FirebaseApp)正在解析。
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);
}
我错过了导入声明吗?是否需要一些额外的配置?
答案 0 :(得分:0)
在FirebaseAdminConfig类中,您直接粘贴了代码段。您需要先创建一个方法,否则会出现编译错误。
@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);
}
}
答案 1 :(得分:0)
您也可以在应用程序的主类中进行
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);
}