Morten Nyhaug
Morten Nyhaug

Reputation: 283

Do I need a firebase service account when I only want to use the emulator?

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?

Upvotes: 5

Views: 1729

Answers (1)

Hiranya Jayathilaka
Hiranya Jayathilaka

Reputation: 7438

You can pass a fake credential like this:

GoogleCredentials.create(new AccessToken(
    "mock-token", 
    Date.from(LocalDateTime.now().plusYears(1).atZone(ZoneId.systemDefault()).toInstant())));

Upvotes: 2

Related Questions