node.js,passport-wordpress:所需的" redirect_uri"参数丢失

时间:2014-10-06 06:03:58

标签: wordpress node.js express oauth-2.0 passport.js

尝试使用passport-wordpress创建演示 https://www.npmjs.org/package/passport-wordpress

passport-wordpress允许您使用wordpress.com上的凭据登录node.js应用程序

我在developer.wordpress.com/apps上设置了我的Wordpress应用程序:

OAuth Information
Client ID <removed>
Client Secret <removed>
Redirect URL  http://greendept.com/wp-pass/
Javascript Origins    http://wp-node2.herokuapp.com
Type  Web
Request token URL https://public-api.wordpress.com/oauth2/token
Authorize URL https://public-api.wordpress.com/oauth2/authorize
Authenticate URL  https://public-api.wordpress.com/oauth2/authenticate

在我的node.js app中:

    var CLIENT_ID = <removed>;
    var CLIENT_SECRET = <removed>;
    passport.use(new WordpressStrategy({
    clientID: CLIENT_ID,
    clientSecret: CLIENT_SECRET
  },
  function(accessToken, refreshToken, profile, done) {
    User.findOrCreate({ WordpressId: profile.id }, function (err, user) {
      return done(err, user);
    });
  }

当我尝试授权时,它会转到此URL(作为一行,为了便于阅读,我在这里分为两行):

  

https://public-api.wordpress.com/oauth2/authorize

     

response_type = code&amp; redirect_uri =&amp; client_id = removed

我可以看到该URL中缺少redirect_uri,因此我收到此错误并不奇怪:

  

请求无效,请返回并重试。

     

错误代码:invalid_request

     

错误消息:缺少必需的“redirect_uri”参数。

我不知道我的代码在哪里或如何提交redirect_uri。

1 个答案:

答案 0 :(得分:1)

您需要传递一个回调网址作为选项。

来自passport-wordpress

The strategy requires a verify callback, which accepts these credentials and 
calls done providing a user, as well as options specifying a client ID, 
client secret, and callback URL.

来自lib/strategy.js

Examples:

  passport.use(new WordpressStrategy({
      clientID: '123-456-789',
      clientSecret: 'shhh-its-a-secret',
      callbackURL: 'https://www.example.net/auth/wordpress/callback'
    },
    function(accessToken, refreshToken, profile, done) {
      User.findOrCreate(..., function (err, user) {
        done(err, user);
      });
    }
  ));