Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase for Android - W/PersistentConnection: pc_0 - Provided authentication credentials are invalid

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).

  1. In Main Activity's onCreate method I read some value from Firebase Database:

FirebaseDatabase.getInstance() .getReference() .child(NODE_USERS).child(user.getUid()).child(NODE_DICTIONARY_VERSION) .addListenerForSingleValueEvent(...);

  1. It works fine. But after some time the method described above goes to some kind of endless loop (no errors, no onCancelled call in ValueEventListener). Also the following message appears in log:
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

  1. do silent login on application startup before accessing Firebase Database:
FirebaseAuth.getInstance().getCurrentUser()
.reauthenticate(credential).addOnCompleteListener(...);
  1. reconnect with Firebase Database:
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.

like image 284
Alexander Bilchuk Avatar asked Dec 19 '16 11:12

Alexander Bilchuk


3 Answers

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);
}
like image 121
Alexander Bilchuk Avatar answered Nov 14 '22 04:11

Alexander Bilchuk


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.

like image 3
Shruti Avatar answered Nov 14 '22 05:11

Shruti


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:

  1. Invalidating the cache and restart
  2. clean project
  3. rebuild project

hope it helps you😁

like image 1
Raptor King Avatar answered Nov 14 '22 05:11

Raptor King