gpt4 book ai didi

node.js - 使用 Node js restify 和 restify-oauth2

转载 作者:搜寻专家 更新时间:2023-11-01 00:08:29 24 4
gpt4 key购买 nike

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

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

关于node.js - 使用 Node js restify 和 restify-oauth2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21084314/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com