适用于Android的Firebase - W / PersistentConnection:pc_0 - 提供的身份验证凭据无效

时间:2016-12-19 11:22:55

标签: android firebase firebase-realtime-database

我在Android项目中使用Firebase(版本10.0.0)并遇到Firebase数据库的以下问题:

前提条件: 用户使用Google帐户通过Firebase Auth登录( FirebaseAuth.getInstance()。getCurrentUser()返回非空值)。

  1. 在主要活动的onCreate方法中,我从Firebase数据库中读取了一些值:
  2. 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

    1. 工作正常。但是经过一段时间后,上述方法会进入某种无限循环(没有错误, ValueEventListener 中没有 onCancelled 调用)。日志中还会显示以下消息:
    2. 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/

      之后,对Firebase数据库的所有其他调用(读取,写入,删除等)也无法正常工作。

      为了解决这个问题,我尝试了

      1. 在访问Firebase数据库之前在应用程序启动时进行静默登录:
      2. FirebaseAuth.getInstance().getCurrentUser()
        .reauthenticate(credential).addOnCompleteListener(...);
        1. 重新连接Firebase数据库:
        2. FirebaseDatabase.getInstance().goOffline();
          FirebaseDatabase.getInstance().goOnline();

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

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

          知道如何解决这个问题吗?或者我可能做错了什么?

          完整的Firebase日志:

          mtcars

          P.S。看起来问题只出现在我的Nexus 5(Android 6.0.1)上。 Sony Xperia V(Android 4.3)没有这样的问题。

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

3 个答案:

答案 0 :(得分:4)

最后,由于Firebase支持工程师,我找到了解决问题的方法! :)

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

因此,以下代码无法正常工作:

// Main Activity
protected void onCreate(...) {
   if (FirebaseAuth.getInstance().getCurrentUser() != null) {
      // accessing Firebase Database
   }
}

必须像这样:

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

答案 1 :(得分:2)

亚历山大的答案对我不起作用..

似乎清除所有 Google Play服务存储解决了问题设置=> Apps => Google Play服务=>存储=>管理存储=>清除所有数据,此问题可能需要由Firebase或Google修复。

答案 2 :(得分:0)

我也遇到了同样的错误,但是造成的是我将google-service.json从旧项目切换到了新项目,但是我的应用仍在连接到旧项目,所以我做到了这一点,并且它解决了它android studio中的缓存问题。

步骤:

  1. 使缓存无效并重新启动
  2. 清洁项目
  3. 重建项目

希望它可以帮助您?