Firebase服务帐户未读

时间:2018-08-17 22:04:43

标签: java firebase firebase-realtime-database

我无法使用Firebase Admin SDK在Java中获取项目ID和其他密钥。我想设置实时数据库,但是,在读取serviceAccount.json文件之后,我没有项目ID,并且无法从数据库中读取数据。

这是我的代码:

public class Main {
public static void main(String[] args) throws Exception {
    try {
        FileInputStream fileInputStream = new FileInputStream("serviceAccountKey.json");

        FirebaseOptions options = new FirebaseOptions.Builder().setCredentials(GoogleCredentials.fromStream(fileInputStream)).setDatabaseUrl("<LINK TO DB>").build();

        System.out.println(" " + options.getProjectId());
        FirebaseApp.initializeApp(options);
    } catch (IOException e){
        System.out.println("Probleem bij het laden van serviceaccount");
        System.out.println(e.getMessage());
        System.exit(1);
    }

    DatabaseReference ref = FirebaseDatabase.getInstance().getReference("messages");

    ref.runTransaction(new Transaction.Handler() {
        @Override
        public Transaction.Result doTransaction(MutableData mutableData) {
            System.out.println("data" + mutableData.getValue().toString());
            return Transaction.success(mutableData);
        }

        @Override
        public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {
            System.out.println("Complete?" + b);
        }
    });
}

我在做什么错了?

0 个答案:

没有答案