【问题标题】:Firebase Android Authentication failed: expired_token (Auth token is expired)Firebase Android 身份验证失败:expired_token(身份验证令牌已过期)
【发布时间】:2016-09-27 01:41:55
【问题描述】:

我遇到了使用com.google.gms:google-services:3.0.0com.google.firebase:firebase-auth:9.0.1 进行Android Firebase 身份验证的问题。

在使用 Firebase(Google 或 Facebook)进行身份验证后 1 小时,我收到以下错误:

W/PersistentConnection: pc_0 - Authentication failed: expired_token (Auth token is expired)

为什么 Firebase 令牌会在 1 小时后过期以及如何延长此过期时间?

更新

我仍然遇到这个问题,Firebase 令牌会在 1 小时后过期。现在我收到以下消息: W/PersistentConnection: pc_0 - Authentication failed: invalid_token (Invalid claim 'kid' in auth header.)

感谢您的帮助。

【问题讨论】:

  • 您好,我正在使用 firebase-auth:9.0.2 并且遇到了同样的问题。请问你找到解决办法了吗?
  • 你找到解决办法了吗?我仍然面临这个问题。

标签: android firebase-authentication google-authentication facebook-authentication


【解决方案1】:

如果我们使用默认的身份验证提供程序,例如(Google、Facebook、Email..),在 Firebase 控制台中更新您的应用程序的“SHA-1 密钥”将解决令牌过期问题。

discussion 中,一位 Google 开发人员分享了解决此问题的指南。

向导:https://drive.google.com/file/d/0B94LePkXiqa6SXVFd3N1NzJHX1E/view

【讨论】:

    【解决方案2】:

    尝试实现FirebaseInstanceIdService获取刷新令牌。

    获取注册令牌

    您可以通过扩展FirebaseInstanceIdService 来访问令牌的值。确保您已将服务添加到您的manifest,然后在onTokenRefresh 的上下文中调用getToken,并记录如下所示的值:

        @Override
    public void onTokenRefresh() {
        // Get updated InstanceID token.
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Log.d(TAG, "Refreshed token: " + refreshedToken);
    
        // TODO: Implement this method to send any registration to your app's servers.
        sendRegistrationToServer(refreshedToken);
    }
    

    onTokenRefreshcallback 会在生成新令牌时触发,因此 在其上下文中调用 getToken 可确保您正在访问 当前可用的注册令牌。 FirebaseInstanceID.getToken() 如果令牌尚未生成,则返回 null。

    代码:

    import android.util.Log;
    
    import com.google.firebase.iid.FirebaseInstanceId;
    import com.google.firebase.iid.FirebaseInstanceIdService;
    
    
    public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
    
        private static final String TAG = "MyFirebaseIIDService";
    
        /**
         * Called if InstanceID token is updated. This may occur if the security of
         * the previous token had been compromised. Note that this is called when the InstanceID token
         * is initially generated so this is where you would retrieve the token.
         */
        // [START refresh_token]
        @Override
        public void onTokenRefresh() {
            // Get updated InstanceID token.
            String refreshedToken = FirebaseInstanceId.getInstance().getToken();
            Log.d(TAG, "Refreshed token: " + refreshedToken);
    
            // TODO: Implement this method to send any registration to your app's servers.
            sendRegistrationToServer(refreshedToken);
        }
        // [END refresh_token]
    
        /**
         * Persist token to third-party servers.
         *
         * Modify this method to associate the user's FCM InstanceID token with any server-side account
         * maintained by your application.
         *
         * @param token The new token.
         */
        private void sendRegistrationToServer(String token) {
            // Add custom implementation, as needed.
        }
    }
    

    希望对你有帮助。

    【讨论】:

    • 谢谢@pRaNaY,我会试试的。为什么 Firebase 身份验证文档中没有提到这一点?旧的 Firebase 控制台允许手动设置令牌有效期,现在我找不到这个选项了。
    • 这里描述的令牌是 FCM 的设备注册令牌 - firebase.google.com/docs/cloud-messaging/android/client
    • 这不是授权令牌。这是针对 fcm 令牌的。两者是完全不同的东西!
    【解决方案3】:

    Firebase 令牌的新最长生命周期为 1 小时 - 我今天早些时候在文档中阅读了它。

    至于在 auth 标头中声明“kid”无效。,我在 Google 上正好得到 2 个搜索结果(:在 Firebase 文档中没有与 kid 相关的文档。我想我们将不得不等待 Google 的答复(或尽可能切换回旧版本的 Firebase)。

    【讨论】:

      【解决方案4】:

      检查最后一个用户是否为空或过期

      GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(context);
              if (account == null || account.isExpired()) {
                  System.out.println("AccountGoogle: null");
                  GoogleSignInClient mGoogleSignInClient = GoogleSignIn.getClient(context, gso);
                  Intent signInIntent = mGoogleSignInClient.getSignInIntent();
                  fragment.startActivityForResult(signInIntent, RC_SIGN_IN);
      
              } 
      

      【讨论】:

        猜你喜欢
        • 2017-11-27
        • 2019-03-09
        • 2022-08-16
        • 2018-07-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多