I'm using Firebase (version 10.0.0) in my Android project and faced with the following problem with Firebase Database:
Precondition: User is logged in via Firebase Auth with Google Account (FirebaseAuth.getInstance().getCurrentUser() returns non null value).
FirebaseDatabase.getInstance()
.getReference()
.child(NODE_USERS).child(user.getUid()).child(NODE_DICTIONARY_VERSION)
.addListenerForSingleValueEvent(...);
W/PersistentConnection: pc_0 - Provided authentication credentials are invalid. This usually indicates your FirebaseApp instance was not initialized correctly. Make sure your google-services.json file has the correct firebase_url and api_key. You can re-download google-services.json from https://console.firebase.google.com/
After that all other calls to Firebase Database (read, write, remove, etc) also don't work.
To fix this I tried to
FirebaseAuth.getInstance().getCurrentUser() .reauthenticate(credential).addOnCompleteListener(...);
FirebaseDatabase.getInstance().goOffline(); FirebaseDatabase.getInstance().goOnline();
As a result, the problem with Firebase Database appears not so often and at least after a day has passed. Also the problem is disappeared for a some time if to restart the whole application (without these fixes the problem reproduces every time, only re-login helps).
Anyway, the problem still exists and application restart is a very bad solution :)
Any idea how to fix this problem? Or maybe I'm doing something wrong?
The full Firebase log:
12-19 13:03:40.544 D/FirebaseAuth: Notifying listeners about user ( HbY7R8FPR6QwgstQd3wmypI2nwJ2 ).
12-19 13:03:40.546 D/FirebaseApp: Notifying auth state listeners.
12-19 13:03:40.546 D/FirebaseApp: Notified 1 auth state listeners.
12-19 13:03:40.546 D/RepoOperation: Auth token changed, triggering auth token refresh
12-19 13:03:40.602 D/FirebaseAuth: Notifying listeners about user ( HbY7R8FPR6QwgstQd3wmypI2nwJ2 ).
12-19 13:03:40.613 D/FirebaseApp: Notifying auth state listeners.
12-19 13:03:40.613 D/FirebaseApp: Notified 1 auth state listeners.
12-19 13:03:40.613 D/RepoOperation: Auth token changed, triggering auth token refresh
12-19 13:03:43.832 V/FA: Session started, time: 176462962
12-19 13:03:43.853 I/FA: Tag Manager is not found and thus will not be used
12-19 13:03:43.858 D/FA: Logging event (FE): _s, Bundle[{_o=auto, _sc=MainActivity, _si=3824046701607023337}]
12-19 13:03:43.885 V/FA: Using measurement service
12-19 13:03:43.885 V/FA: Connecting to remote service
12-19 13:03:43.909 D/FA: Connected to remote service
12-19 13:03:43.910 V/FA: Processing queued up service tasks: 1
12-19 13:03:44.139 W/PersistentConnection: pc_0 - Provided authentication credentials are invalid. This usually indicates your FirebaseApp instance was not initialized correctly. Make sure your google-services.json file has the correct firebase_url and api_key. You can re-download google-services.json from https://console.firebase.google.com/.
12-19 13:03:45.985 W/PersistentConnection: pc_0 - Provided authentication credentials are invalid. This usually indicates your FirebaseApp instance was not initialized correctly. Make sure your google-services.json file has the correct firebase_url and api_key. You can re-download google-services.json from https://console.firebase.google.com/.
12-19 13:03:48.945 V/FA: Inactivity, disconnecting from the service
P.S. It looks like the problem appears on my Nexus 5 (Android 6.0.1) only. There is no such problem with Sony Xperia V (Android 4.3).
P.P.S. I also tried to build debug and release apks and to add "Editor" role to all autogenerated service accounts in google cloud console - it does not help either.
Finally I found how to fix the problem thanks to the Firebase support engineers! :)
The trick is to wait for getting FirebaseAuth from FirebaseAuth.AuthStateListener (instead of getting it directly from the FirebaseAuth.getInstance()) prior to accessing Firebase Database at application startup: it looks like Firebase needs some time to initialize/renew user authentication.
So, the following code doesn't work correctly:
// Main Activity
protected void onCreate(...) {
if (FirebaseAuth.getInstance().getCurrentUser() != null) {
// accessing Firebase Database
}
}
It must be like the this:
protected void onCreate(...) {
//...
authStateListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// accessing Firebase Database
}
}
};
FirebaseAuth.getInstance().addAuthStateListener(authStateListener);
}
Alexander's answer did not work for me..
Seems that clearing all Google Play services storage solves the problem Settings => Apps => Google Play services => Storage => MANAGE STORAGE => CLEAR ALL DATA, this issue probably needs to be fixed by Firebase or Google.
I was also getting the same error but what created is that i switched the google-service.json from old project to new but my app was still connecting to the old one so i did this and it solved it maybe there was some cache problem in android studio.
Steps:
hope it helps you😁
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With