4

When connecting to the Firebase Emulator Suite, using the java admin SDK, how can I initialize the app without existing service account credentials? I have the environment variable FIREBASE_DATABASE_EMULATOR_HOST=localhost:9000 set, and the emulator is running. I then initialize the app with the following code:

FirebaseApp.initializeApp(FirebaseOptions.builder()
        .setCredentials(GoogleCredentials.getApplicationDefault())
        .setDatabaseUrl("http://localhost:9000?ns=my-project-id")
        .build(), "my-project-id");

But I get the exception:

java.io.IOException: The Application Default Credentials are not available. 
They are available if running in Google Compute Engine. 
Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined 
pointing to a file defining the credentials. 
See https://developers.google.com/accounts/docs/application-default-credentials for more information.

Do I always need to have an existing service-account even if I only want to use the emulator? Is there some sort of dummy service-account-credentials for the emulator that I can use?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Morten Nyhaug
  • 263
  • 2
  • 13

1 Answers1

2

You can pass a fake credential like this:

GoogleCredentials.create(new AccessToken(
    "mock-token", 
    Date.from(LocalDateTime.now().plusYears(1).atZone(ZoneId.systemDefault()).toInstant())));
Morten Nyhaug
  • 263
  • 2
  • 13
Hiranya Jayathilaka
  • 7,180
  • 1
  • 23
  • 34
  • When the expirationTime is null you get a NullPointerException. But if you set an expirationTime, then this works :) – Morten Nyhaug Aug 11 '21 at 12:52
  • This didn't work for me with 8.1.0 when calling something like firebaseAuth.getUserByEmal() - it always returned a 401. It turned out that the token string is appended to the http request Bearer header, and it works if you use "owner" as the string I figured this out from a comment here: https://stackoverflow.com/questions/64845486/delete-all-users-from-the-new-firebase-auth-emulator – trouserboycott Nov 03 '21 at 11:12