简体   繁体   English

Firebase Android 身份验证失败:expired_token(身份验证令牌已过期)

[英]Firebase Android Authentication failed: expired_token (Auth token is expired)

I encounter an issue with Android Firebase Auth using com.google.gms:google-services:3.0.0 and com.google.firebase:firebase-auth:9.0.1 .我在使用com.google.gms:google-services:3.0.0com.google.firebase:firebase-auth:9.0.1遇到了 Android Firebase com.google.firebase:firebase-auth:9.0.1

1 hour after authentication with Firebase (Google or Facebook), I get the following error:使用 Firebase(Google 或 Facebook)进行身份验证后 1 小时,我收到以下错误:

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

Why does Firebase token expire after 1 hour and how to extend this expiration period?为什么 Firebase 令牌会在 1 小时后过期以及如何延长此有效期?

UPDATE更新

I still encounter this issue, Firebase token expires after 1 hour.我仍然遇到这个问题,Firebase 令牌在 1 小时后过期。 Now I get the following message: W/PersistentConnection: pc_0 - Authentication failed: invalid_token (Invalid claim 'kid' in auth header.)现在我收到以下消息: W/PersistentConnection: pc_0 - Authentication failed: invalid_token (Invalid claim 'kid' in auth header.)

I appreciate any help.我很感激任何帮助。

If we use default Auth providers like (Google, Facebook, Email..), updating "SHA-1 key" of your Application in firebase console would fix the token expiry issue.如果我们使用默认的身份验证提供程序(例如(Google、Facebook、电子邮件...),在 firebase 控制台中更新您的应用程序的“SHA-1 密钥”将解决令牌过期问题。

In this discussion a Google developer shared a guide to solve this problem.在本次讨论中,一位 Google 开发人员分享了解决此问题的指南。

Guide: https://drive.google.com/file/d/0B94LePkXiqa6SXVFd3N1NzJHX1E/view指南: https : //drive.google.com/file/d/0B94LePkXiqa6SXVFd3N1NzJHX1E/view

Try to implement FirebaseInstanceIdService to get refresh token.尝试实现FirebaseInstanceIdService以获取刷新令牌。

Access the registration token :访问注册令牌

You can access the token's value by extending FirebaseInstanceIdService .您可以通过扩展FirebaseInstanceIdService来访问令牌的值。 Make sure you have added the service to your manifest , then call getToken in the context of onTokenRefresh , and log the value as shown:请确保您添加的服务,您的清单,然后调用getToken的背景下onTokenRefresh ,并记录值,如下所示:

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

The onTokenRefreshcallback fires whenever a new token is generated, so calling getToken in its context ensures that you are accessing a current, available registration token.每当生成新令牌时onTokenRefreshcallback 都会触发,因此在其上下文中调用getToken可确保您正在访问当前可用的注册令牌。 FirebaseInstanceID.getToken() returns null if the token has not yet been generated.如果令牌尚未生成,则FirebaseInstanceID.getToken()返回 null。

Code:代码:

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.
    }
}

I hope its helps you.我希望它可以帮助你。

The new maximum life time for Firebase Tokens is 1 hour - I read it in the docs earlier today. Firebase 令牌的新最长生命周期为 1 小时 - 我今天早些时候在文档中阅读了它。

As for Invalid claim 'kid' in auth header.至于身份验证标题中的无效声明“孩子”。 , I get exactly 2 search results on Google for that (: No documentation related to kid in Firebase docs. I guess we will have to wait for answers from Google (or switch back to the old version of Firebase if possible). ,我在谷歌上得到了 2 个搜索结果(:Firebase 文档中没有与孩子相关的文档。我想我们将不得不等待谷歌的答案(或者如果可能的话切换回旧版本的 Firebase)。

check if the last user is null or expired检查最后一个用户是否为空或已过期

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

        } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM