我不熟悉Express JS和Node JS。
我打算在Express Middleware中手动实现一个身份验证服务器,我正在使用https://github.com/ranm8/requestify发出请求
const requestify = require('requestify');
app.use(function (req, res, next) {
var config = require('./config-' + process.env.REACT_APP_ENV)
var redirect = config.ADMIN_UI_URL
// Perform login to Auth-Server
if (req.originalUrl.startsWith('/?code')) {
let auth_code = req.query.code
let params = {
'grant_type': 'authorization_code',
'code': auth_code,
'client_id': config.OAUTH_CLIENT_ID,
'redirect_uri': redirect
}
requestify.post(config.OAUTH_ID_URL+'/oauth2/token', params).then(function(response) {
console.log(response.getBody()); // There is not data post to the Auth-Server
});
return res.redirect('http://google.com');
}
// Check Token
if (req.cookies._token === undefined) {
let url = config.OAUTH_ID_URL + '/oauth2/authorize?' + 'response_type=code' + '&' + 'client_id=' + config.OAUTH_CLIENT_ID + '&' + 'redirect_uri=' + redirect
return res.redirect(url)
}
next()
})我可以检查用户令牌并接收auth_code。但我无法通过向Auth-User发出post请求来请求已登录用户的令牌
似乎我没有理解NodeJS、Epxress是如何工作的,或者这里的一些范围
请帮帮我!谢谢
发布于 2017-08-07 13:39:28
尝试检查您得到的错误是什么。像这样添加错误处理程序
requestify.post(config.OAUTH_ID_URL+'/oauth2/token', params).then(function(response) {
console.log(response.getBody()); // There is not data post to the Auth-Server
}).error(function(error){
console.log(error) // see if anything is coming in error object
});发布于 2017-08-07 15:17:53
我的错,我应该使用https://www.npmjs.com/package/request#forms包
request.post({url:config.OAUTH_ID_URL+'/oauth2/token', form: bodyData},
function(err,httpResponse,body){
console.log(err);
console.log(body);
})它现在起作用了!谢谢
https://stackoverflow.com/questions/45539512
复制相似问题