firebase java,Firebase Admin Java SDK不做任何操作

I've setup the firebase admin SDK using this guide

So I'm initializing it in the following way.

InputStream refreshToken = new ClassPathResource(CONFIG_FILE).getInputStream();

FirebaseOptions options = new FirebaseOptions.Builder()

.setCredential(FirebaseCredentials.fromRefreshToken(refreshToken))

.setDatabaseUrl("https://.firebaseio.com/")

.build();

FirebaseApp.initializeApp(options);

I got no errors to this point, everything seems fine. However, I'm unable to do any operations. Calling the methods does nothing. Eg:

FirebaseDatabase.getInstance()

.getReference()

.addListenerForSingleValueEvent(new ValueEventListener() {

@Override

public void onDataChange(DataSnapshot dataSnapshot) {

//this callback is never called

}

@Override

public void onCancelled(DatabaseError databaseError) {

}

});

or

class Pojo {

public Pojo(String name, String surname) {

this.name = name;

this.surname = surname;

}

String name;

String surname;

//omitted getters and setters

}

FirebaseDatabase.getInstance.getReference()

.push()

.setValue(new Pojo("Test", "Test"));

//this value is never set

It simply looks like that the skd ignores any command. Any ideas?

Background: I'm running this as a web application (spring) on a local machine.

解决方案

If you are authenticating the SDK with a service account key JSON file as documented in Add Firebase to your app, you should use the fromCertificate() method, not the fromRefreshToken() method:

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

FirebaseOptions options = new FirebaseOptions.Builder()

.setCredential(FirebaseCredentials.fromCertificate(serviceAccount))

.setDatabaseUrl("https://.firebaseio.com/")

.build();

FirebaseApp.initializeApp(options);

Authenticating with a refresh token is a bit of an advanced use case and is probably not what you want to be using.

Firebase是一个由Google提供的移动和Web应用开发平台,它提供了一系列工具和基础架构,帮助开发者构建高质量的应用程序、扩大用户群并实现盈利。Firebase提供了多种功能,包括实时数据库、身份验证、云存储、云函数、消息推送等。 在Java中使用Firebase,可以使用Firebase Admin Java SDK来访问Firebase服务。该SDK允许从Java的特权环境(例如服务器或云)访问Firebase服务。您可以使用Firebase Admin Java SDK来管理用户身份验证、读写实时数据库、存储和检索文件等。 要使用Firebase Admin Java SDK,您需要在项目中添加相应的依赖项。您可以在项目的构建文件中添加以下依赖项: ```java dependencies { // Firebase Admin SDK implementation 'com.google.firebase:firebase-admin:7.0.0' } ``` 然后,您可以在Java代码中使用Firebase Admin Java SDK的各种功能。例如,以下代码演示了如何使用Firebase Admin Java SDK创建一个新用户: ```java import com.google.firebase.auth.FirebaseAuth; import com.google.firebase.auth.UserRecord; import com.google.firebase.auth.UserRecord.CreateRequest; import com.google.firebase.auth.UserRecord.CreateRequest.User; public class FirebaseExample { public static void main(String[] args) throws Exception { // 初始化Firebase Admin SDK FirebaseAuth firebaseAuth = FirebaseAuth.getInstance(); // 创建一个新用户 CreateRequest request = new CreateRequest() .setEmail("user@example.com") .setPassword("password") .setDisplayName("John Doe") .setDisabled(false); UserRecord userRecord = firebaseAuth.createUser(request); System.out.println("Successfully created new user: " + userRecord.getUid()); } } ``` 上述代码使用Firebase Admin Java SDK创建了一个新用户,并打印出了新用户的UID。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值