首页 > 解决方案 > 为什么我收到此 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);

标签: node.jsfacebook

解决方案


现在我看到了问题。我的路线/facebook/callback是调用PassportService.authenticateGithubCallback. 我将其更改为 Facebook 回调,现在它可以工作了。


推荐阅读