node.js - 为什么我收到此 Facebook Passport oauth2 响应错误?
问题描述
我正在尝试使用护照(节点 js)将 Facebook 登录集成到我的应用程序中。我在重定向到 facebook 时看到我的应用程序,我可以接受,并且在重定向时,我收到此错误:传递的代码不正确或已过期。
我的代码是:
passport.use(new FacebookStrategy({
clientID: process.env.FACEBOOK_APP_ID || '',
clientSecret: process.env.FACEBOOK_APP_SECRET || '',
callbackURL: hostUrl + "/api/auth/facebook/callback"
},
async function(accessToken, refreshToken, profile, cb) {
console.log(profile);
}
));
// PassportService.ts
public static redirectToHomepage(req: Request, res: Response) {
if (req.user) {
const user: any = req.user;
const token = jwt.sign({userId: user.id}, process.env.JWT_SECRET || 'abc');
res.redirect('/?token='+token);
}
}
public static authenticateFacebook(req: Request, res: Response, next: NextFunction) {
passport.authenticate('facebook')(req, res, next);
}
public static authenticateFacebookCallback(req: Request, res: Response, next: NextFunction) {
passport.authenticate('facebook', { failureRedirect: '/auth/login' })(req, res, next);
}
//routes.ts. endpoint: /api/auth
router.get('/facebook', PassportService.authenticateFacebook);
router.get('/facebook/callback', PassportService.authenticateGithubCallback, PassportService.redirectToHomepage);
解决方案
现在我看到了问题。我的路线/facebook/callback
是调用PassportService.authenticateGithubCallback
. 我将其更改为 Facebook 回调,现在它可以工作了。
推荐阅读
- c# - 使用异步代码时使用 [ThreadStaticAttribute] 缓存 StringBuilder 是否安全?
- javascript - 访问未授予或过期 GMAIL API 和 Oauth2 / 如何使用 GMAIL API 和 GSuite 域设置电子邮件签名
- python - 带有生成器表达式的 Python for 循环对于已经存在的字典是否值得?
- python - 向量化 Pandas tqdm progress_apply 函数
- flutter - 如何将方形图像放入圆形装饰盒 FLUTTER
- angular - Angular 库项目的路径别名不工作
- c# - 将 HTML 表单转换为 ASP.Net MVC Razor @Html.BeginForm
- angular - 当我使用 Drilldown(Highcharts) 时,我的构建 --prod 在角度 8 中变得非常大
- google-cloud-platform - 如何在 google 计算引擎和 google bigquery 之间创建专用网络?
- c - 为什么要在这个 flex 程序上过早地使用 eof?