WP OAuth 服务器和 Node JS 出现 "You need to provide a redirectUri" 错误

Getting "You need to provide a redirectUri" error with WP OAuth Server & NodeJS

我正在创建一个 NodeJS 应用程序,我正在尝试使用 Wordpress 通过 OAuth2 进行身份验证,但我 运行 遇到了一些问题。我正在利用 'passport-wpoauth' 模块。

我在尝试授权时遇到错误 "You need to provide a redirectUri"。我确实尝试在传递给 WPOAuthStrategy 的参数中指定 "redirectUri",但这没有帮助。据我所知,问题发生在对 passport.authenticate() 的调用中。

代码如下,想知道您是否有任何建议?

const bodyParser = require("body-parser");
const express = require('express');
const expressSession = require('express-session');
const url = require('url');
const passport = require('passport');
const OAuth2Strategy = require('passport-oauth2');
const WPOAuthStrategy = require('passport-wpoauth');

function initPassport(app) {

    app.use(passport.initialize());
    app.use(passport.session());

    passport.serializeUser(function(user, done) {
        done(null, user);
    });

    passport.deserializeUser(function(user, done) {
        done(null, user);
    });

    var oauthConfig = {
      "clientID": "Wq3lkiaPfMn83Cd5YWNoRAHpJtN55a",
      "clientSecret": "82AbnUuyJJk7RoK4uJ0h7SSPhBi5Ho",
      "authorizationURL": "https://localhost/oauth/authorize",
      "tokenURL": "https://localhost/oauth/token/",
      "callbackURL": "http://localhost:7070/api/auth/callback",
      "userProfileURL": "http://localhost/myprofile"
    };

    passport.use(new WPOAuthStrategy(oauthConfig,
        function(accessToken, refreshToken, profile, callback) {
            console.log('profile', profile);
            callback(undefined, {
                id: '56757dsfe22',
                email: 'dummy@example.com'
            });
        }
    ));

    app.get('/auth/callback',
        passport.authenticate('wpoauth', { failureRedirect: '/api/login' }),
        function(req, res) {
            console.log('success');
            // Successful authentication, redirect home.
            res.redirect('/');
        });    
}

function handleAuthenticate(req, res, next) {
    console.log('xxxx', 'handleAuthenticate');
    passport.authenticate('wpoauth')(req, res, next);
}

function initRoutes(app) {
    app.get(/.*/, handleAuthenticate) ;

    app.get('/auth/callback',
        passport.authenticate('wpoauth', { failureRedirect: '/api/login' }),
        function(req, res) {
            console.log('success');
            // Successful authentication, redirect home.
            res.redirect('/');
        }); 
}

var app = express();
var router = express.Router();

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

initPassport(app);

app.use('/api/', router);
initRoutes(router);

请注意,我也曾尝试直接使用 passport-oauth2 模块,一直到登录屏幕,但一旦登录并返回 http://localhost:7070/api/auth/callback URL,我就会收到错误消息:

TokenError: Authorization code doesn't exist or is invalid for the client

clientID 和 clientSecret 都与 WordPress 管理员 UI 中为 'WP OAuth Server' 指定的相匹配。

使用 WP OAuth 服务器 3.2.0001,Nginx 和 WP 4.6.1

查看“passport-wpoauth”包的源代码表明 'redirectUri' 应该作为请求正文的一部分,而不是像我期望的那样来自作为选项传入的设置.

我将代码更改为:

app.get('/auth/wordpress', function(req, res, next) {
    req.body.redirectUri = 'http://redirectUrl/'
    passport.authenticate('oauth2', {})(req, res, next);
});

由于其他不兼容问题,这个问题比较严重。我已经向该项目提交了拉取请求。