The setup we have forbids a direct call to https://login.microsoftonline.com and hence I created a Proxy API in Azure APIM with a policy as follows:
Policy Type: Inbound
Get & set-variables from header (scope, client id, secret & grant type)
send-request to https://login.microsoftonline.com
set-method to POST
set-header Content-Type to x-www-form-urlencoded
set-body to return scope, client id, secret and grant type
get the response body
get the access token
set the token in a new body (liquid template)
set the body in return-response (convert body to string)
In APIM, I test this with all the required headers and I get an access token without any issues.
In Postman, I call my proxy API with all the required headers and I get an access token without any issues.
{
"status": "ok",
"token": "Bearer ASODIA@#$)(*ASDJASODNADSAOSDJ....PROPER TOKEN"
}
Now I do the same in my NodeJS code, call the same API with Headers (using Axios), I do get a proper response back BUT the Token returns empty!
{
"status": "ok",
"token": "Bearer "
}
Anybody has any idea on why this is happening and how I can solve this? If it was a CORS issue, I should have got an error instead of 200 OK and partial body! Or am I wrong in assuming that?
Thanks in advance...
SSG
Edit: NodeJS Code for the API Call
router.get("/test", async (req, res) => {
let config = {
headers: {
client_id: process.env["BID"],
client_secret: process.env["BSEC"],
scope: process.env["BSCOPE"],
},
};
let data = {
"Content-Type": "application/json",
};
const apimUrl = "https://gateway-test.hapi.hmgroup.com/hapi-utils-auth/token";
axios
.get(apimUrl, data, config)
.then((response) => {
console.log(response.data);
res.send(response.data);
})
.catch((error) => {
console.log(error);
});
});
I have added the below given policy to the Echo API which gets created by default when we create an Azure APIM instance. I am able to fetch the Bearer token using this policy.
I am able to get the bearer token as shown below-
Now I am trying to call the APIM Url in the NodeJS code to get the token.
app.js-
sample.js-
I am able to get the token successfully.
Follow my steps, you will get the response too.