
I am trying to call the Events API from an Azure function using node.js
I have created the web application by following
I got the api endpoint and token.
I also registered my Azure function url as the origin when creating the web application.
For example if my function app url is
FnApp Url
https://msazfunctionapp.azurewebsites.net/api/msazfunctionname?code=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=
In the origin field I registered
https://msazfunctionapp.azurewebsites.net/
After creating the web application , I followed below link to use Events API in an Azure function
https://learn.microsoft.com/en-us/dynamics365/marketing/developer/using-events-api
But when I run the function from azure portal using Code Test tab , I see the below error in response
const https = require('https');
module.exports = async function (context, req) {
try {
context.log('JavaScript HTTP trigger function for D365 Events API processed a request.');
try {
const requestOptions = {
method : 'GET',
headers : { 'Content-Type': 'application/json' }
};
const fetchUrl = 'https://xxxxxxxxxxxxxxxxxxx.svc.dynamics.com/EvtMgmt/api/v2.0/events/published?emApplicationtoken=xxxxx-xxxxx-xxxxxxxxx’;
const response = await fetch(fetchUrl);
context.log('Response status : ' response.status);
const respData = await response.json();
const JSONrespData = JSON.stringify(respData);
context.log('Response data as string : ' respData);
context.log('Response data as Object : ' , respData);
context.log('Response data JSON Stringified : ' JSONrespData);
} catch(err) {
context.log('Error in Events Api : ' err);
}
// context.res = data;
const responseJSON = {
"data" : respData
}
context.res.headers = { "Content-Type": "application/json" };
context.res.headers = { "content-type": "application/json" };
context.res.body = responseJSON;
context.done();
} catch(err) {
context.res = {
status: 500
};
}
}HTTP 400 Bad Request."