当我只想使用模拟器时,是否需要 firebase 服务帐户?

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

使用 java admin SDK 连接到 Firebase Emulator Suite 时,如何在没有现有服务帐户凭据的情况下初始化应用程序?我设置了环境变量FIREBASE_DATABASE_EMULATOR_HOST=localhost:9000,模拟器是运行。然后我使用以下代码初始化应用程序:

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

但我遇到了异常:

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.

我是否总是需要有一个现有的服务帐户,即使我只想使用模拟器?我可以使用模拟器的某种虚拟服务帐户凭证吗?

您可以像这样传递伪造的凭据:

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