我正在为我的移动应用程序使用flutter。我试着用谷歌添加登录。对于颤动侧来说一切都很好。我正在从移动应用程序获取idToken并发送到我的后端nodejs。
现在,我想通过google-auth- idToken包在nodejs后台验证用户的请求。
let token = "token"
const CLIENT_ID = "client_id"
const { OAuth2Client } = require('google-auth-library');
const client = new OAuth2Client(CLIENT_ID);
async function verify() {
try {
const ticket = await client.verifyIdToken({
idToken: token,
audience: CLIENT_ID, // Specify the CLIENT_ID of the app that accesses the backend
// Or, if multiple clients access the backend:
//[CLIENT_ID_1, CLIENT_ID_2, CLIENT_ID_3]
});
const payload = ticket.getPayload();
const userid = payload['sub'];
console.log(payload)
} catch (error) {
console.log(error)
}
}
verify()但此代码始终返回以下错误: Invalid token signature: at OAuth2Client.verifySignedJwtWithCertsAsync (\node_modules\google-auth-library\build\src\auth\oauth2client.js:566:19)
在nodejs后台验证这个idToken应该怎么做?
谢谢。
发布于 2021-10-24 10:33:26
如果传递给函数的idToken来自flutter应用程序的日志,则由于print()的限制,很可能无法在日志中打印整个idToken。
我使用下面的代码片段打印出idToken,并将其用于给我一个成功响应的API中。
print('ID TOKEN');
String token = googleAuth.idToken;
while (token.length > 0) {
int initLength = (token.length >= 500 ? 500 : token.length);
print(token.substring(0, initLength));
int endLength = token.length;
token = token.substring(initLength, endLength);
}https://stackoverflow.com/questions/65369080
复制相似问题