草庐IT

android - 适用于 Android 的 Firebase - W/PersistentConnection : pc_0 - Provided authentication credentials are invalid

coder 2023-12-02 原文

我在我的 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. 它工作正常。但一段时间后,上述方法进入某种无限循环(没有错误,ValueEventListener 中没有 onCancelled 调用)。日志中还会出现以下消息:
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

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

附言我还尝试构建调试和发布 apk,并向谷歌云控制台中的所有自动生成的服务帐户添加“编辑器”角色——这也无济于事。

最佳答案

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

关于android - 适用于 Android 的 Firebase - W/PersistentConnection : pc_0 - Provided authentication credentials are invalid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41221491/

有关android - 适用于 Android 的 Firebase - W/PersistentConnection : pc_0 - Provided authentication credentials are invalid的更多相关文章

随机推荐