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

Ale*_*huk 6 android firebase firebase-realtime-database

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

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

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

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

  1. 它工作正常.但一段时间后,上述方法进入某种无限循环的(没有错误,没有onCancelled呼叫ValueEventListener).日志中还会显示以下消息:
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数据库之前在应用程序启动时执行静默登录
FirebaseAuth.getInstance().getCurrentUser()
.reauthenticate(credential).addOnCompleteListener(...);
  1. 重新连接Firebase数据库:
FirebaseDatabase.getInstance().goOffline();
FirebaseDatabase.getInstance().goOnline();

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

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

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

完整的Firebase日志:

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
Run Code Online (Sandbox Code Playgroud)

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

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

Ale*_*huk 4

感谢 Firebase 支持工程师,我终于找到了解决问题的方法!:)

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

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

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

一定是这样的:

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);
}
Run Code Online (Sandbox Code Playgroud)