【问题标题】:Using node js restify and restify-oauth2使用节点 js restify 和 restify-oauth2
【发布时间】:2014-01-31 18:53:29
【问题描述】:

我是开发Web服务的初学者,我有一个带有node js和模块restify的服务器,我想使用模块OAuth2-restify添加身份验证。但是当我努力准备获取令牌的请求时。我使用节点 restify 客户端进行请求。请有人可以给我一些例子。文档说

如果在 Authorization 标头中提供了有效令牌,则 req.username 是真实的,并且 令牌端点,完全由 Restify–OAuth2 管理。它为给定的客户端 ID/客户端密码/用户名/密码组合生成令牌。 但是我如何获得令牌?这是文档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);
});

【问题讨论】:

  • 这个你搞定了吗?

标签: node.js restify


【解决方案1】:

尝试使用以下参数向您的请求添加 POST 正文:

grant_type = client_credentials

【讨论】:

    【解决方案2】:

    您已使用基本身份验证向“令牌”端点发送 POST 请求,正如 Jordy 所说,添加 grant_type 参数:

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

    【讨论】:

      猜你喜欢
      • 2013-08-18
      • 2013-11-18
      • 2012-10-12
      • 1970-01-01
      • 2023-04-08
      • 2015-10-06
      • 1970-01-01
      • 2020-02-18
      • 2014-06-12
      相关资源
      最近更新 更多