如何使用Node.js中的请求模块为OAuth2编写POST请求

时间:2019-07-23 09:24:22

标签: node.js api oauth-2.0 postman

我正在用nodejs开发一种邮递员类型的应用程序。我试图找出OAuth2请求的POST / GET / PUT / DELETE api。我发现有npm的-

[1] client-oauth2
[2] simple-oauth2
[3] passport-oauth2 

但是,我不想发出OAuth2请求。我只想像邮递员一样测试它。我制作了一个api,如果我写错了,请纠正我。

function postAuthRequest(req, res) {
    var deferred = Q.defer();

    console.log("Inside postAuthRequest ");
    console.log("req.body = ", req.body)

       var url = req.body.url

    var data = req.body.requestBody
var token = req.body.TokenName;
var auth_Url = req.body.authUrl;
var client_ID = req.body.clientID;
var client_Secret = req.body.clientSecret;
var access_Token = req.body.accessToken;
var grant_type = req.body.grant;



    request.post({
        headers: {
                'content-type': 'application/x-www-form-urlencoded',
                TokenName : token,
        authorizationURL: : auth_Url,
        clientID: client_ID, 
        clientSecret: client_Secret,
        tokenURL: access_Token,
        grant_type: grant_type

        },
        url: url,
        body: data
    }, function (error, response, body) {
        if (error) {
            console.log(error);
        } else {
            console.log(response);
            deferred.resolve(response);
        }
    });

    return deferred.promise;


}

0 个答案:

没有答案