How do I connect to firebase emulator running in docker using Java?

Viewed 161

I managed to get a local version of firebase emulator running, using this. Everything seems to be running fine, and I'm even able to access the UI at http://localhost:4000. Here's a screenshot following docker run

enter image description here

Now, what I'd like to do is to connect to this locally emulated Firebase app, using a service account json file (Which can normally be downloaded from google cloud console). This is the code snippet using which I'm using to test.

public class GenericFcmTests {

    private static FirebaseOptions firebaseOptions;
    private static FirebaseApp firebaseApp;

    private static final Logger log = LoggerFactory.getLogger(GenericFcmTests.class.getName());

    @BeforeClass
    public static void setup() throws IOException {
        try (InputStream inputStream = Objects.requireNonNull(FirebaseMessagingServiceImpl.class.getClassLoader().getResourceAsStream("serviceAccountKey.json"))) {
            firebaseOptions = FirebaseOptions.builder()
                    .setCredentials(GoogleCredentials.fromStream(inputStream))
                    .build();
            firebaseApp = FirebaseApp.initializeApp(firebaseOptions);
        } catch (IOException e) {
            log.error("Initialization failure: ", e);
        }
    }

    @Test
    public void testGetFireBaseInstance() throws FirebaseMessagingException {
        var messaging = FirebaseMessaging.getInstance(firebaseApp);
        var message = Message.builder()
                .setTopic("test-topic")
                .setNotification(Notification.builder().setTitle("Test tile").setBody("Test body").build())
                .build();
        var sent = messaging.send(message, true);
        assertFalse(StringUtils.isEmpty(sent));
        System.out.println(messaging);
    }
}

serviceAccountKey.json looks something like this:

{
  "type": "service_account",
  "project_id": "demo-project",
  "private_key_id": "abc",
  "private_key": "-----BEGIN PRIVATE KEY-----\private key\n-----END PRIVATE KEY-----\n",
  "client_email": "firebase-adminsdk-asdfgt566@demo-project.iam.gserviceaccount.com",
  "client_id": "clientId",
  "auth_uri": "http://localhost:9199",
  "token_uri": "http://localhost:9199/token"
}

Now, when I run the test, I get this error:

com.google.firebase.messaging.FirebaseMessagingException: Unknown error while making a remote service call: Error getting access token for service account: Connection refused (Connection refused), iss: firebase-adminsdk-asdfgt566@demo-project.iam.gserviceaccount.com

How can I create a serviceAccountKey json file for local emulation access?

0 Answers
Related