Firebase verifyIdToken Error: Decoding Firebase ID token failed。
•浏览 1
Firebase verifyIdToken Error: Decoding Firebase ID token failed.
我正在尝试设置一个与 NodeJS REST API 通信的 Android 应用程序,该 API 与 Firebase 通信,但似乎 firebase 令牌对我不起作用。
我现在在本地运行一个本地 nodejs 服务器和一个 Android 模拟来测试它,它的外观如下:
安卓方法:
@Override
public void onNewToken(String token) {
Log.i(TAG,"Refreshed token:" + token);
postRegistrationToken(token);
}Refreshed token:"MY_TOKEN_STRING"app.post('/api/token/get', (req, res) => {
const token = req.body.registrationToken;
LOG.info(`Acquired token: ${token}`);
admin.auth().verifyIdToken(token)
.then((decodedToken) => {
LOG.info(`Successfully validated registrationToken ${token}`);
//stuff
})
.catch((err) => {
LOG.error(err)
});
});Acquired token:"MY_TOKEN_STRING"
Error: Decoding Firebase ID token failed.
Make sure you passed the entire string JWT which represents an ID token.
See https://firebase.google.com/docs/auth/admin/verify-id-tokens for details on how to retrieve an ID token.
这会正确生成并发送到 REST API。
日志显示:
@Override
public void onNewToken(String token) {
Log.i(TAG,"Refreshed token:" + token);
postRegistrationToken(token);
}Refreshed token:"MY_TOKEN_STRING"app.post('/api/token/get', (req, res) => {
const token = req.body.registrationToken;
LOG.info(`Acquired token: ${token}`);
admin.auth().verifyIdToken(token)
.then((decodedToken) => {
LOG.info(`Successfully validated registrationToken ${token}`);
//stuff
})
.catch((err) => {
LOG.error(err)
});
});Acquired token:"MY_TOKEN_STRING"
Error: Decoding Firebase ID token failed.
Make sure you passed the entire string JWT which represents an ID token.
See https://firebase.google.com/docs/auth/admin/verify-id-tokens for details on how to retrieve an ID token.
在 NodeJS 中:
@Override
public void onNewToken(String token) {
Log.i(TAG,"Refreshed token:" + token);
postRegistrationToken(token);
}Refreshed token:"MY_TOKEN_STRING"app.post('/api/token/get', (req, res) => {
const token = req.body.registrationToken;
LOG.info(`Acquired token: ${token}`);
admin.auth().verifyIdToken(token)
.then((decodedToken) => {
LOG.info(`Successfully validated registrationToken ${token}`);
//stuff
})
.catch((err) => {
LOG.error(err)
});
});Acquired token:"MY_TOKEN_STRING"
Error: Decoding Firebase ID token failed.
Make sure you passed the entire string JWT which represents an ID token.
See https://firebase.google.com/docs/auth/admin/verify-id-tokens for details on how to retrieve an ID token.
verifyIdToken 失败,但令牌与 Android 客户端中生成的令牌相同:
@Override
public void onNewToken(String token) {
Log.i(TAG,"Refreshed token:" + token);
postRegistrationToken(token);
}Refreshed token:"MY_TOKEN_STRING"app.post('/api/token/get', (req, res) => {
const token = req.body.registrationToken;
LOG.info(`Acquired token: ${token}`);
admin.auth().verifyIdToken(token)
.then((decodedToken) => {
LOG.info(`Successfully validated registrationToken ${token}`);
//stuff
})
.catch((err) => {
LOG.error(err)
});
});Acquired token:"MY_TOKEN_STRING"
Error: Decoding Firebase ID token failed.
Make sure you passed the entire string JWT which represents an ID token.
See https://firebase.google.com/docs/auth/admin/verify-id-tokens for details on how to retrieve an ID token.
我尝试在 JWT 验证器网站(如 https://www.jsonwebtoken.io/)中验证令牌,但他们说这是无效签名。
我是否遗漏了什么或有什么问题?
您似乎正在客户端上获取 Firebase 实例 ID 令牌(也称为 FCM 令牌),然后尝试验证它是否是有效的 Firebase 身份验证 ID 令牌。
- FCM 令牌/实例 ID 令牌标识特定设备上特定应用程序的安装。它不识别特定用户。
- Auth ID 令牌标识特定应用程序/项目的特定用户。它不识别特定设备。
这两个令牌完全不同,用途不同,Authentication Admin SDK 无法正确验证 FCM 令牌。
要获取 Firebase 身份验证 ID 令牌,您应该调用 FirebaseUser.getIdToken()。有关这方面的示例,请参阅有关获取 ID 令牌的文档。