簡體   English   中英

使用節點js restify和restify-oauth2

[英]Using node js restify and restify-oauth2

我是開發Web服務的初學者,我有一個服務器,節點js和模塊restify,我想使用模塊OAuth2-restify添加身份驗證。 但是當我努力准備獲取令牌的請求時。我使用節點restify客戶端來發出請求。請有人給我一些例子。 文件說

如果Authorization標頭中提供了有效令牌,則req.username是真實的,令牌端點完全由Restify-OAuth2管理。 它為給定的客戶端ID /客戶端密鑰/用戶名/密碼組合生成令牌。 但我怎么得到一個令牌? 這是documentacion https://github.com/domenic/restify-oauth2當我測試服務寬度一個restify-client我得到這個錯誤{“error”:“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);
});

嘗試使用以下參數為您的請求添加POST正文:

grant_type = client_credentials

您已使用基本身份驗證向“令牌”端點發送POST請求,並且正如Jordy所說添加grant_type參數:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM