В моем коде ниже я пытаюсь вызвать API, и из этого API я вызываю другой API, используя Request.Есть ли способ возврата из второго вызова для первого вызова.
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 обратно вызывающей его функции, а затем вернуть ее обратно вызывающему клиенту.получить / защитить конечную точку.