Java Firebase 管理员缺少 GoogleCredentials

Java Firebase-Admin missing GoogleCredentials

我想制作一个可以访问我的 Firebase 数据库的服务器应用程序。

我遵循了这个指南:https://firebase.google.com/docs/admin/setup#initialize_the_sdk

但我收到以下错误:

cannot resolve GoogleCredentials

FileInputStream serviceAccount = new 
   FileInputStream("path/to/serviceAccountKey.json");

FirebaseOptions options = new FirebaseOptions.Builder()
   .setCredentials(GoogleCredentials.fromStream(serviceAccount))
   .setDatabaseUrl("https://<DATABASE_NAME>.firebaseio.com/")
   .build();

FirebaseApp.initializeApp(options);

编辑:

导入以下库后:

  • org.json.jar
  • netty-all-4.0.0.final.jar
  • google-http-client-1.16.0-rc.jar
  • google-http-client-1.16.0-rc-sources.jar
  • firebase-admin-5.9.0.jar
  • google-auth-library-oauth2-http-0.9.0.jar
  • google-auth-library-credentials-0.9.0.jar
  • google-api-client-1.23.0.jar
  • google-api-client-1.4.1-beta.jar
  • guava-24.0-jre.jar
  • google-http-client-jackson2-1.23.0.jar
  • jackson-core-2.9.4.jar
  • slf4j-api-1.7.25.jar
  • slf4j-jdk14-1.7.25.jar
  • api-common-1.3.9.jar

我卡在了这个错误:

Feb 27, 2018 2:15:33 PM com.google.firebase.database.logging.LogWrapper error SCHWERWIEGEND: Uncaught exception in Firebase Database runloop (5.9.0). Please report to firebase-database-client@google.com java.lang.NoSuchMethodError: io.netty.channel.nio.NioEventLoopGroup.(ILjava/util/concurrent/Executor;)V at com.google.firebase.database.connection.NettyWebSocketClient.(NettyWebSocketClient.java:71) at com.google.firebase.database.connection.WebsocketConnection$DefaultWSClientFactory.newClient(WebsocketConnection.java:435) at com.google.firebase.database.connection.WebsocketConnection.(WebsocketConnection.java:80) at com.google.firebase.database.connection.WebsocketConnection.(WebsocketConnection.java:68) at com.google.firebase.database.connection.Connection$DefaultWebsocketConnectionFactory.newConnection(Connection.java:297) at com.google.firebase.database.connection.Connection.(Connection.java:72) at com.google.firebase.database.connection.Connection.(Connection.java:57) at com.google.firebase.database.connection.PersistentConnectionImpl$DefaultConnectionFactory.newConnection(PersistentConnectionImpl.java:1303) at com.google.firebase.database.connection.PersistentConnectionImpl.openNetworkConnection(PersistentConnectionImpl.java:547) at com.google.firebase.database.connection.PersistentConnectionImpl.access0(PersistentConnectionImpl.java:37) at com.google.firebase.database.connection.PersistentConnectionImpl.onSuccess(PersistentConnectionImpl.java:500) at com.google.firebase.database.core.Context.onSuccess(Context.java:67) at com.google.firebase.database.core.JvmAuthTokenProvider.getToken(JvmAuthTokenProvider.java:66) at com.google.firebase.database.core.Context.getToken(Context.java:62) at com.google.firebase.database.connection.PersistentConnectionImpl.run(PersistentConnectionImpl.java:490) at com.google.firebase.database.connection.util.RetryHelper.run(RetryHelper.java:93) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access1(ScheduledThreadPoolExecutor.java:180) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745)

你需要 Netty 4.1.x。我建议依赖 4.1.17.Final,这是 Admin SDK 所依赖的。您的依赖管理工具(Maven、Gradle 等)应该会为您处理这个问题。

下载此 JAR 并将其包含为外部库

https://mvnrepository.com/artifact/io.netty/netty-all/4.0.0.Final