0

I am trying to retrieve data from a Firebase Realtime database using the Firebase Admin SDK in java (in a java server context). The authentication is done using the service account. I followed all the steps in the official documentation. In the following snippet, the callback onDataChange is supposed to be called at least one time to give access to the initial snapshot.

Every instruction is silently done, so it suggests everything is right (authentication, connection to the database), but this callback is never called.

Do you have any idea of what is going wrong?

final CountDownLatch latch = new CountDownLatch(1);
GoogleCredentials credentials = GoogleCredentials.fromStream(serviceAccount);
FirebaseOptions options = FirebaseOptions.builder()
    .setCredentials(credentials)
    .setDatabaseUrl( getDBUrl() )
    .setDatabaseAuthVariableOverride(auth)
    .build();
FirebaseApp app = FirebaseApp.initializeApp(options);
DatabaseReference dbRef = FirebaseDatabase.getInstance().getReference( ref );
dbRef.addValueEventListener(new ValueEventListener() {
            public void onDataChange(DataSnapshot ds) {
                System.out.println("onDataChange");
                latch.countDown();
            }

            public void onCancelled(DatabaseError de) {
                System.out.println("onCancelled");
                latch.countDown();
            }
        });

latch.await();
7
  • Are you waiting for the latch to finish anywhere? Commented Jun 10, 2021 at 13:33
  • Sorry, I didn't mention the initialization and the await. the latch is initialized this way: final CountDownLatch latch = new CountDownLatch(1); and the method ends with an latch.await();
    – Thibaut
    Commented Jun 10, 2021 at 14:10
  • Please edit your question to include that code. There's an edit link right under it. Commented Jun 10, 2021 at 14:33
  • 1
    1) My code is running on a local test server. 2) ref is equal to "LOGS". It is the path of the data I would like to browse 3) The getDBUrl() returns "https://<my_project_name>.firebaseio.com/". This URL is the one shown in the Realtime database explorer in the admin panel. It seems to be a US instance (us-central1) 4) Nothing is printed.
    – Thibaut
    Commented Jun 10, 2021 at 15:13
  • 1
    This code snippet runs in a test method of a Spring web project. The same kind of code is also in a controller async method that uses DeferredResult to handle the async response (which never comes).
    – Thibaut
    Commented Jun 10, 2021 at 16:12

1 Answer 1

0

After some investigation, conflict between Google libraries was the root cause of this issue. I don't exactly know which ones (but it should be related to oauth and credentials). The project I am working on already used Google libraries to handle Android management API and Google Cloud platform. I upgraded all of them and the issue is solved.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.