![](/img/trans.png)
[英]How to use google-auth-library with Google Cloud Storage with NodeJS?
[英]How to resolve Nodejs google-auth-library invalid token signature error?
我正在为我的移动应用程序使用 flutter。 我尝试添加使用谷歌登录。 Flutter 方面一切正常。 我正在从移动应用程序获取 idToken 并发送到我的后端 nodejs。
现在,我想使用这个 idToken 在 nodejs 后端使用 google-auth-library package 验证用户的请求。
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()
但是此代码总是返回此错误 => 错误:无效的令牌签名:在 OAuth2Client.verifySignedJwtWithCertsAsync (\node_modules\google-auth-library\build\src\auth\oauth2client.js:566:19)
我应该怎么做才能在nodejs后端验证这个idToken?
谢谢。
如果您传递给 function 的 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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.