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



当连接到Firebase模拟器套件时,使用java管理SDK,我如何在没有现有服务帐户凭证的情况下初始化应用程序?我设置了环境变量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())));

最新更新