TheGodProject
TheGodProject

Reputation: 147

Firebase Admin SDK with Java

I am trying to setup firebase admin sdk in a Java project. Following the steps at https://firebase.google.com/docs/admin/setup#add_the_sdk I successfully added the sdk by adding the dependency to my build.gradle using,

dependencies {
    compile 'com.google.firebase:firebase-admin:5.0.0'
}

then ran gradle build which returned BUILD SUCCESSFUL.

The next step in the guide uses multiple Firebase classes which I do not know where to locate. It tells me to initialize the SDK by using:

FirebaseOptions options = new FirebaseOptions.Builder()
    .setCredential(FirebaseCredentials.fromCertificate(serviceAccount))
    .setDatabaseUrl("https://<DATABASE_NAME>.firebaseio.com/")
    .build();

FirebaseApp.initializeApp(options);

The guide doesn't show where to import these Firebase classes from either. I assume the classes were downloaded after I ran build.gradle but I cant seem to locate any of these classes. Does anyone know what location the gradle should've downloaded it to or what import I must use?

Upvotes: 3

Views: 9017

Answers (4)

njari
njari

Reputation: 192

You have to initialise a bean with this snippet.

How I'm doing it ->

@Configuration
public class FirebaseAppConfig {

    @SneakyThrows
    public FirebaseAppConfig() {
        FileInputStream serviceAccount =
                new FileInputStream("filepath");

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

        FirebaseApp.initializeApp(options);

    }


}

This is the bean that provides the auth for your operations. The classes can be imported directly from the mvn/gradle dependency but they NEED to be combined with auth to work.

Upvotes: 0

jazeb007
jazeb007

Reputation: 678

Simply download the JAR file from mvnrepository and include it in your project. https://mvnrepository.com/artifact/com.google.firebase/firebase-admin/8.0.1

Upvotes: 0

mnhmilu
mnhmilu

Reputation: 2468

The guide need more precise contents and example.

I have integrated Firebase with my current Spring boot project by following these blog savicprvoslav/Spring-Boot-starter , it was out dated too as using new Firebase sdk is strongly recommended. You can check my question and answer here with sample updated code. Stakoverflow The blog suggested to use this code within a configuration file (with @Configuration annotation ).

Upvotes: 2

Sam Stern
Sam Stern

Reputation: 25134

The Java quickstart may be a good place to look.

The Database.java file has some of the imports you're looking for:

import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.auth.FirebaseCredentials;
import com.google.firebase.database.*;

Upvotes: 1

Related Questions