在下面的代码中,我试图调用一个API,然后使用Request从该API调用另一个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 / secure端点的客户端。
答案 0 :(得分:0)
尝试将回调函数传递给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);
}