node.js - 请求调用使用 Request 的函数

标签 node.js node-modules

在下面的代码中,我尝试调用一个 API,并从该 API 使用 Request 调用另一个 API。有没有办法将第二次调用的返回值提供给第一次调用。

app.get("/secure", function (req, response) {
    console.log("Call to /secure");
    /*
    Call introspect and verify if token is valid
    */
    var accessToken  = req.headers.access_token;

    var headers = {
        'Content-Type':     'application/x-www-form-urlencoded',
        'accept': 'application/json',
        'authorization': 'Basic MG9hNTl6cTcyZ0I4eWVQYkwzNTY6S1E2MlpsMHR2MWtyRC1LS2Nid0hEaTB6TUVSODJkai1xX3NnNUVoZA=='   
    }

    // Configure the options for the introspect end point
    var options = {
        url: 'https://xyz.com.example/oauth2/aus4wijly1L6nfeEY356/v1/introspect',
        method: 'POST',
        headers: headers,
        body: 'token='+accessToken+'&token_type_hint=access_token'
    }



    // Start the request
    request(options, function (error, res, body) {
        if (!error && res.statusCode == 200) {
            // Print out the response body
            console.log(body)
            if (JSON.parse(body).active == true){
                console.log("Success");     
                getUserInfo(accessToken, function (err, resp, body){
                    if(!err && resp!=null){
                        console.log("Asycn call");
                        response.send("hurray");
                    } else {
                        console.log("Error calling userinfo");
                    }
                });

            } else {
                response.send("Token not valid");
            }
        } else {
            response.send(body);
        }
    })

});

function getUserInfo(accessToken){
    //Create request for the /userinfo end point
    var userinfoheaders = {
        'Content-Type':     'application/x-www-form-urlencoded',
        'accept': 'application/json',
        'authorization': 'Bearer '+accessToken
    }

    // Configure the request
    var userinfooptions = {
        url: 'https://xyz.com.example/oauth2/aus4wijly1L6nfeEY356/v1/userinfo',
        method: 'POST',
        headers: userinfoheaders
    }
    // End of configuration for userinfo request
    // Call the userinfo end point
    // Start the request
    request(userinfooptions, function (error, resp, body) {
        if (!error && resp.statusCode == 200) {
            console.log("userinfo called")
            console.log(body)
            return JSON.parse(body).displayName;
        } else {
            return "Error";
        }
    });
}

目标是将 displayName 返回给调用它的函数,然后将其返回给调用 get to/secure 端点的客户端。

最佳答案

尝试将回调函数传递给 getUserInfo。

app.get("/secure", function (req, response) {
  console.log("Call to /secure");
  /*
     Call introspect and verify if token is valid
  */
  var accessToken  = req.headers.access_token;

  var headers = {
    'Content-Type':     'application/x-www-form-urlencoded',
    'accept': 'application/json',
    'authorization': 'Basic MG9hNTl6cTcyZ0I4eWVQYkwzNTY6S1E2MlpsMHR2MWtyRC1LS2Nid0hEaTB6TUVSODJkai1xX3NnNUVoZA=='   
  }

  // Configure the options for the introspect end point
  var options = {
    url: 'https://xyz.com.example/oauth2/aus4wijly1L6nfeEY356/v1/introspect',
    method: 'POST',
    headers: headers,
    body: 'token='+accessToken+'&token_type_hint=access_token'
  }



  // Start the request
  request(options, function (error, res, body) {
    if (!error && res.statusCode == 200) {
        // Print out the response body
        console.log(body)
        if (JSON.parse(body).active == true){
            console.log("Success");     
            getUserInfo(accessToken, function (err, resp, body){
                if (!err && resp.statusCode == 200) {
                    response.send(JSON.parse(body).displayName);
                } else {
                    console.log("Error calling userinfo");
                }
            });
        } else {
            response.send("Token not valid");
        }
    } else {
        response.send(body);
    }
  })  
});


function getUserInfo(accessToken, callback){
  //Create request for the /userinfo end point
  var userinfoheaders = {
    'Content-Type':     'application/x-www-form-urlencoded',
    'accept': 'application/json',
    'authorization': 'Bearer '+accessToken
  }

  // Configure the request
  var userinfooptions = {
    url: 'https://xyz.com.example/oauth2/aus4wijly1L6nfeEY356/v1/userinfo',
    method: 'POST',
    headers: userinfoheaders
  }
  // End of configuration for userinfo request
  // Call the userinfo end point
  // Start the request
  request(userinfooptions, callback);
 }

关于node.js - 请求调用使用 Request 的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53572305/

相关文章:

javascript - Electron.js 应用程序在一个小时未使用后卡住

node.js - cross-env 命令在 nodejs 中如何工作?

node.js - Node : could not initialize ICU (check NODE_ICU_DATA or --icu-data-dir parameters)

node.js - npm:何时使用 `--force` 和 `--legacy-peer-deps`

node.js - 在 Docker 镜像中复制/node_modules 不是一个好主意吗?

version-control - 一次开发多个 npm 模块的方法(具有交叉依赖)

javascript - 使用 promise 中的值的异步函数

node.js - Nexmo 不在 node.js 中发送短信

node.js - 在没有 $unwind 的情况下查找数组内的重复项

javascript - 如何将全栈应用程序部署到heroku或netlify?哪些文件是必需的?