2014-01-13 28 views
3

我是開發Web服務的初學者,我有一個帶有節點js和模塊restify的服務器,並且我想使用模塊OAuth2-restify添加身份驗證。但是,當我努力準備獲取令牌的請求時。我使用節點restify客戶端進行請求。請有人提供一些示例。文檔說使用節點js restify和restify -oauth2

如果在授權標頭中提供了有效令牌,則req.username是truthy並且是完全由Restify-OAuth2管理的令牌端點。它爲給定的客戶端ID /客戶端密碼/用戶名/密碼組合生成令牌。 但我怎麼得到一個令牌?這裏是documentacion https://github.com/domenic/restify-oauth2 當我測試服務隊寬度a的RESTify客戶端我得到這個錯誤 { 「錯誤」: 「INVALID_REQUEST」, 「ERROR_DESCRIPTION」: 「必須提供身體」}

here is my code: 
--------------------------------------Server-------------------------------- 
    var SERVER_PORT = 8800; 
    var restify = require('restify'); 
    var server = restify.createServer({ 
    name: "Example Restify-OAuth2 Client Credentials Server", 
    //version: require("../../package.json").version, 
    formatters: { 
     "application/hal+json": function (req, res, body) { 
      return res.formatters["application/json"](req, res, body); 
     } 
    } 
    }); 

    server.use(restify.authorizationParser()); 
    server.use(restify.queryParser()); 
    server.use(restify.bodyParser({ mapParams: false })); 
    var restifyOAuth2 = require("restify-oauth2"); 
    var hooks = require("./hooks"); 
    restifyOAuth2.cc(server, { tokenEndpoint:"/token", hooks: hooks }); 

    //server.use(restify.acceptParser(server.acceptable)); 

    var handlers = require('./handlers'); 
    handlers.setHandlers(server); 
    server.listen(SERVER_PORT); 
--------------------------------------handlers-------------------------------- 
    module.exports = { 

    setHandlers: function(server) 
     { 
      var restify = require('restify'); 
      var token=function(req,res,next) { 
          owner=req.body.owner; 
          password=req.password.owner; 
          if(req.username) 
            res.send({result:"sucess"}); 

        } 
     server.get("/token",token); 

    } 
    } 
-----------------------------client restify for test services-------------------------------- 
    var restify = require('restify'); 
    var client = restify.createJsonClient({ 
    url: 'http://localhost:8800', 
    version: '*' 
}); 

    client.post('/token', { client_id: 'officialApiClient',client_secret: officialApiClient'}, function(err, req, res, obj) { 
    //assert.ifError(err); 
    //console.log('%d -> %j', res.statusCode, res.headers); 
    console.log('%j', obj); 
}); 
+0

你有想過這個嗎? – gmaniac

回答

0

嘗試添加POST體你的要求有以下參數:

grant_type = client_credentials 
0

您發送POST請求使用基本身份驗證,併爲約迪的「令牌」端點該增加grant_type參數:

POST /oauth/token 
Authorization: Basic Y2xpZW50SWQ6c2VjcmV0 
Content-Type: application/x-www-form-urlencoded;charset=UTF-8 
grant_type=client_credentials