当我使用 passportjs 的新 OAuthStrategy() 来验证 twitter 的 3 条腿的 auth 时,我无法进入验证回调。

问题描述 投票:0回答:1

我想使用Oauth1.o从提供商那里获取凭证,但我被卡在了半路上。我正在使用 passportjs 的实现。

下面是简单的代码。

  1. oauth.js
    const passport = require('passport');
    const OAuthStrategy = require('passport-oauth').OAuthStrategy;

    passport.use(
        'provider',
        new OAuthStrategy({
            requestTokenURL: "****",
            accessTokenURL: "****",
            userAuthorizationURL: "****",
            consumerKey: "****",
            consumerSecret: "****",
            callbackURL: "****",
        },
        (token, tokenSecret, profile, done) => {
            store[token] = tokenSecret;
            console.log(profile);
            done(null, profile);
        })
    );

    module.exports = passport;
  1. 快递小程序(模块化路由)
const express = require('express');
const router = express.Router();

const passport = require('./oauth');

/**
 * Initialize Passport
 * Restore authentication state, if any, from the session
 */
router.use(passport.initialize());
router.use(passport.session());

router.get('/oauth/sign-in', passport.authenticate('provider'));

router.get('/oauth/callback', (req, res) => {
    const { oauth_token, oauth_verifier, denied } = req.query;

    if (denied !== null && denied !== undefined) {
        req.session.destroy();
        res.json(denied);
    }

    res.json({ oauth_token, oauth_verifier });
});

module.exports = router;

从上面的代码中,我在Twitter页面上授权应用后,成功进入回调URL。但我无法 console.log 的 token,tokenSecret & profile。文档中我也不清楚如何使用 done 回调函数。

我正在学习使用 https:/github.comtwitterdevtwauth-webblobmastertwauth-web.py。 并希望避免使用 passport-twitter 来学习掌握他们给出的例子。

node.js express passport.js oauth-1.0a
1个回答
0
投票

你需要在回调路径中添加认证中间件。你可以查看文档 此处:

router.get('/oauth/callback', passport.authenticate('provider', {
  failureRedirect: '/login' //optional
}),
  (req, res) => {
    //...your code...
  });
© www.soinside.com 2019 - 2024. All rights reserved.