适用于Android的Firebase - W / PersistentConnection:pc_0 - 提供的身份验证凭据无效 [英] Firebase for Android - W/PersistentConnection: pc_0 - Provided authentication credentials are invalid

查看:406
本文介绍了适用于Android的Firebase - W / PersistentConnection:pc_0 - 提供的身份验证凭据无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android项目中使用Firebase(版本10.0.0),并遇到以下Firebase数据库问题: 前提条件:
用户通过Firebase身份验证与Google帐户( FirebaseAuth.getInstance()。getCurrentUser()返回非空值)登录。


  1. 在主Activity的onCreate方法中,我从Firebase数据库读取了一些值:


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


    1. 但是经过一段时间,上面描述的方法进入了某种无限循环(没有错误,在 ValueEventListener 中没有调用)。日志中还显示以下消息:



     W / PersistentConnection:pc_0  - 提供的认证凭证无效。 
    这通常表示您的FirebaseApp实例未正确初始化。
    请确保您的google-services.json文件具有正确的firebase_url和api_key。
    您可以从https://console.firebase.google.com/

    重新下载google-services.json。到Firebase数据库(读取,写入,删除等)也无法正常工作。

    为了解决这个问题,我尝试了
    $ b $在访问Firebase数据库之前,b


    1. 在应用程序启动时执行无提示登录:


       FirebaseAuth。 getInstance()。getCurrentUser()
      .reauthenticate(凭证).addOnCompleteListener(...);




      1. 与Firebase数据库重新连接:



       FirebaseDatabase.getInstance()。goOffline(); 
      FirebaseDatabase.getInstance()。goOnline();

      因此,Firebase数据库的问题并不经常出现,至少在一天过去了。如果重新启动整个应用程序,这个问题也会消失一段时间(没有这些修复,每次都会重现问题,只有重新登录才有帮助)。

      无论如何,问题仍然存在,应用程序重新启动是一个非常糟糕的解决方案:)

      任何想法如何解决这个问题?或者,也许我做错了什么?



      完整的Firebase日志:

        12-19 13:03:40.544 D / FirebaseAuth:通知听众关于用户(HbY7R8FPR6QwgstQd3wmypI2nwJ2)。 
      12-19 13:03:40.546 D / FirebaseApp:通知授权状态侦听器。
      12-19 13:03:40.546 D / FirebaseApp:通知1个身份验证状态侦听器。
      12-19 13:03:40.546 D / RepoOperation:Auth令牌改变,触发授权令牌刷新
      12-19 13:03:40.602 D / FirebaseAuth:通知收听者有关用户(HbY7R8FPR6QwgstQd3wmypI2nwJ2)。
      12-19 13:03:40.613 D / FirebaseApp:通知授权状态侦听器。
      12-19 13:03:40.613 D / FirebaseApp:已通知1个身份验证状态侦听器。
      12-19 13:03:40.613 D / RepoOperation:Auth令牌改变,触发授权令牌刷新
      12-19 13:03:43.832 V / FA:会话启动,时间:176462962
      12-19 13:03:43.853 D / FA:记录事件(FE):_s,Bundle [{ V / FA:使用测量服务
      12-19 13:03:43.885 V / FA:正在连接到__ = auto,_sc = MainActivity,_si = 3824046701607023337}]
      12-19 13:03:43.885远程服务
      12-19 13:03:43.909 D / FA:连接远程服务
      12-19 13:03:43.910 V / FA:处理排队服务任务:1
      12 -19 13:03:44.139 W / PersistentConnection:pc_0 - 提供的认证凭证无效。这通常表示您的FirebaseApp实例未正确初始化。确保你的google-services.json文件具有正确的firebase_url和api_key。您可以从https://console.firebase.google.com/重新下载google-services.json。
      12-19 13:03:45.985 W / PersistentConnection:pc_0 - 提供的认证凭证无效。这通常表示您的FirebaseApp实例未正确初始化。确保你的google-services.json文件有正确的firebase_url和api_key。您可以从https://console.firebase.google.com/重新下载google-services.json。
      12-19 13:03:48.945 V / FA:不活跃,与服务断开

      PS看起来问题只出现在我的Nexus 5(Android 6.0.1)上。索尼Xperia V(Android 4.3)没有这样的问题。



      P.P.S。我还试图构建调试和发布apks,并添加编辑器角色,在谷歌云控制台中的所有自动生成的服务帐户 - 这也没有帮助。

      解决方案最后,我发现如何解决这个问题,感谢Firebase支持工程师! :)

      诀窍是等待从FirebaseAuth.AuthStateListener获取 FirebaseAuth (而不是直接从 FirebaseAuth.getInstance())在应用程序启动时访问Firebase数据库 :看起来Firebase需要一些时间来初始化/更新用户身份验证。



      所以,下面的代码无法正常工作:

        (...){
      if(FirebaseAuth.getInstance()。getCurrentUser()!= null){
      //访问Firebase数据库
      }
      }

      它必须像这样:

        protected void onCreate(...){
      // ...
      authStateListener = new FirebaseAuth.AuthStateListener(){
      @Override
      public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth){
      FirebaseUser user = firebaseAuth.getCurrentUser();
      if(user!= null){
      //访问Firebase数据库
      }
      }
      };
      FirebaseAuth.getInstance()。addAuthStateListener(authStateListener);
      }


      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.

      解决方案

      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);
      }
      

      这篇关于适用于Android的Firebase - W / PersistentConnection:pc_0 - 提供的身份验证凭据无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

      查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆