我正在使用oauth2orize和passport在nodejs中创建一个API,但是当我请求 token 时,client_id参数未定义。
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
为什么客户端参数未定义?
非常感谢
您需要创建至少一个如下所示的护照策略:
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)
});
}
));
然后您需要绑定(bind)您的路线,以便您的请求能够通过抛出此策略:(带有express)
app.post('/url', passport.authenticate('clientPassword'), server.token());
最后,要请求您的 token ,POST 参数必须是:
- client_id
- client_secret
- 用户名
- 密码
- grant_type:密码
我是一名优秀的程序员,十分优秀!