2014-10-10 23 views
1

我使用oauth2orize和passport在nodejs中创建了一个API,但是当我询问令牌时,client_id参数未定义。client_id grant_type = password(oauth2orize)undefinded

Oauth2orize:

server.exchange(oauth2orize.exchange.password(function (client, username, password, scope, callback) { 
    console.log(client); // This is undefined 

帖子:

grant_type: password, 
client: id_client, // I tried with client_id, clientId, id... 
username: username, 
password: password 

为什么客户端参数是不确定的?

非常感谢

回答

0

您需要创建至少一个护照策略是这样的:

var passport = require('passport'), 
    ClientPasswordStrategy = require('passport-oauth2-client-password').Strategy ; 

passport.use("clientPassword", new ClientPasswordStrategy(
    function (clientId, clientSecret, done) { 
     Clients.findOne({clientId: clientId}, function (err, client) { 
      if (err) return done(err) 
      if (!client) return done(null, false) 
      if (!client.trustedClient) return done(null, false) 

      if (client.clientSecret == clientSecret) return done(null, client) 
      else return done(null, false) 
     }); 
    } 
)); 

然后,你需要绑定你的路线,使您的申请将通过举办这次战略:(含快递)

app.post('/url', passport.authenticate('clientPassword'), server.token()); 

最后,请你的道理,POST参数必须是:

  • CLIENT_ID
  • client_secret
  • 用户名
  • 密码
  • grant_type:密码
相关问题