首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在中间件过程中发出Post请求

在中间件过程中发出Post请求
EN

Stack Overflow用户
提问于 2017-08-07 12:38:33
回答 2查看 62关注 0票数 1

我不熟悉Express JS和Node JS。

我打算在Express Middleware中手动实现一个身份验证服务器,我正在使用https://github.com/ranm8/requestify发出请求

代码语言:javascript
复制
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是如何工作的,或者这里的一些范围

请帮帮我!谢谢

EN

回答 2

Stack Overflow用户

发布于 2017-08-07 13:39:28

尝试检查您得到的错误是什么。像这样添加错误处理程序

代码语言:javascript
复制
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
});
票数 0
EN

Stack Overflow用户

发布于 2017-08-07 15:17:53

我的错,我应该使用https://www.npmjs.com/package/request#forms

代码语言:javascript
复制
request.post({url:config.OAUTH_ID_URL+'/oauth2/token', form: bodyData}, 
    function(err,httpResponse,body){
        console.log(err);
        console.log(body);
    })

它现在起作用了!谢谢

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45539512

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档