在node-restify-oauth2-mongodb的初始设置上是invalid_client

我的问题类似于这个问题,没有接受答案: 使用node-restify-oauth2-mongodb的问题

不过,我的clientKeys集合是正确命名的,或者至less我认为是有点不同的。

当我进入我的mongo实例,看我的collections我有:

 clientkeys users 

当我看到这些集合中的东西时,我看到:

 > db.clientkeys.find().pretty() { "_id" : ObjectId("51c6e846ede91c0b8600005e"), "clientName" : "Test Client", "client" : "test", "secret" : "password" } > db.users.find().pretty() { "_id" : ObjectId("543d7b974f1870f131c6d0aa"), "name" : "Test", "email" : "test@test.com", "username" : "tester1", "hashed_password" : "$2a$10$gpMXyCuILBbMF2K6Aroc7.lKoIpuGhvL98ZGGoE0tEOtYSQaCpMde", "role" : "Admin", "__v" : 0 } 

我遵循回购的方向: https : //github.com/rgallagher27/node-restify-oauth2-mongodb

所以我运行这个:

 curl --data "name=Test&email=test@test.com&username=tester1&password=testing27&vPassword=testing27&role=Admin" http://localhost:8090/register 

它回应他如何说,像这样:

 { "__v":0, "name":"Test", "email":"test@test.com", "username":"tester1", "hashed_password":"$2a$10$3uwD9IiKVlkQJvdQXqm07uQnfcXae3AGjDh.zil8.8CgtlQ2MuACK", "_id":"520774753472d74e2c000001", "role":"Admin" } 

然后我运行以下命令:

 curl --user test:password --data grant_type=password --data username=tester1 --data password=testing27 http://localhost:8090/token 

然而这返回:

 { "error":"invalid_client", "error_description":"Client ID and secret did not validate." } 

我不能完全弄清楚我错过了什么。 除非我为我的客户clientkeys使用错误的集合名称,但我不认为我是。

感谢您提供任何帮助!

所以在经过这个过程之后,我发现在hooks.js他们收到了validateClient行35和grantUserToken 52行的错误参数。

这是你将需要改变,使这个应用程序的工作。

35行

 exports.validateClient = function (clientId, clientSecret, cb) 

 exports.validateClient = function (clientCredentials, req, cb) 

第39行

 Client.findOne({ client: clientId, secret: clientSecret }, function (err, client) { 

 Client.findOne({ client: clientCredentials.clientId, secret: clientCredentials.clientSecret }, function (err, client) { 

52行

 exports.grantUserToken = function (username, password, cb) 

 exports.grantUserToken = function (allCredentials, req, cb) 

54行

 var query = User.where( 'username', new RegExp('^' + username + '$', 'i') ); 

 var query = User.where( 'username', new RegExp('^' + allCredentials.username + '$', 'i') ); 

61行

 } else if (user.authenticate(password)) { 

 } else if (user.authenticate(allCredentials.password)) { 

65行和66行

 var token = generateToken(username + ":" + password); var newToken = new Token({ username: username, token: token }); 

 var token = generateToken(allCredentials.username + ":" + allCredentials.password); var newToken = new Token({ username: allCredentials.username, token: token }); 

第70行

 redisClient.set(token, username); 

 redisClient.set(token, allCredentials.username); 

希望这能帮助别人,让我知道别人是否有更好的解决scheme。 这对我有效。

现在当我运行这个

 curl --user test:password --data grant_type=password --data username=tester1 --data password=testing27 http://localhost:8090/token 

这是我的回应

 { "access_token":"S7QEwABRiv6HCSaXy70mUlxY7i/Un/EcgWpvbrBhXlw=", "token_type":"Bearer" } 

现在仍然在hooks.js的秘密路线改变80行

 exports.authenticateToken = function (token, cb) 

 exports.authenticateToken = function (token, req, cb) 

如果你运行这个命令

 curl -H "Authorization:Bearer Your-Bearer-Token" http://localhost:8090/secret 

你的回应将是

 { "message":"Success" } 

快乐的编码!

我使用相同的代码..它的工作原理..我认为你没有正确插入模型的ClientKey ..反馈消息清楚。

它应该是 :

 db.clientkeys.insert({ clientName:"Test Client", client:"test", secret:"password" }) 

请注意收集客户端密钥的名称, 不是ClientKey和 ClientKeys ..它应该遵循模块名称+ s ..