【问题标题】:Firebase for Android - W/PersistentConnection: pc_0 - Provided authentication credentials are invalidFirebase for Android - W/PersistentConnection: pc_0 - 提供的身份验证凭据无效
【发布时间】:2017-05-04 10:33:32
【问题描述】:

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

前提条件: 用户使用 Google 帐户通过 Firebase 身份验证登录(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 - 提供的身份验证凭据无效。
这通常表明您的 FirebaseApp 实例未正确初始化。
确保您的 google-services.json 文件具有正确的 firebase_url 和 api_key。
您可以从 https://console.firebase.google.com/ 重新下载 google-services.json

此后对 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

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

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

【问题讨论】:

    标签: android firebase firebase-realtime-database


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      似乎清除所有 Google Play 服务 存储解决了问题设置 => 应用程序 => Google Play 服务 => 存储 => 管理存储 => 清除所有数据,这个问题可能需要 Firebase 或 Google 来解决。

      【讨论】:

      • 这也解决了我的问题,但我想知道这是否会影响我用户的性能。您是否知道我们是否可以通过编程方式解决此问题,而无需他们手动清除数据?
      • 我也在找这个。这种情况很少发生,但必须解决
      【解决方案3】:

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

      步骤:

      1. 使缓存失效并重启
      2. 清理项目
      3. 重建项目

      希望对你有帮助?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-18
        • 2020-04-20
        • 2018-11-28
        • 2020-07-22
        • 2021-11-29
        相关资源
        最近更新 更多