Firebase Android认证失败:expired_token(认证令牌已过期)

13

我使用com.google.gms:google-services:3.0.0com.google.firebase:firebase-auth:9.0.1出现了Android Firebase Auth的问题。

在与Firebase(Google或Facebook)验证后一个小时后,我会收到以下错误:

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,我也遇到了同样的问题。请问你找到解决方案了吗? - Allan
你找到解决方案了吗?我仍然面临这个问题。 - Aritra Roy
4个回答

3

0

Firebase令牌的最大生存时间现在为1小时-我今天早些时候在文档中读到了这一点。

至于auth header中的无效声明'kid',我在Google上只得到了2个搜索结果(:没有Firebase文档相关的kid。我想我们将不得不等待来自Google的答案(或者如果可能的话切换回Firebase的旧版本)。


0

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

访问注册令牌

您可以通过扩展FirebaseInstanceIdService来访问令牌的值。确保已将服务添加到您的清单文件中,然后在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);
}

每当生成新令牌时,onTokenRefresh回调函数会触发,因此在其上下文中调用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控制台允许手动设置令牌过期时间,现在我找不到这个选项了。 - kandroid
这里描述的令牌是用于FCM的设备注册令牌 - https://firebase.google.com/docs/cloud-messaging/android/client - nathaniel.camomot
3
这不是授权令牌,而是用于 FCM 令牌的。两者完全不同! - kirtan403

0

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

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

        } 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接