I have built a .NET library project for getting an access token for an integration with a third party and I have added the dll file in my D365FO project. I am now trying to use that access token and sending a POST request from x++ to that third party URL but I am getting 400 bad request error in my response.
I have also tested in postman and it's working fine.
Here is my code and anyone please share your valuable suggestion of what I am doing wrong.
{
//Library class for getting access token
AADAuthClient.Program program = new AADAuthClient.Program();
System.Net.WebHeaderCollection headers;
System.Net.HttpWebRequest request;
System.Net.HttpWebResponse response;
str requestBody;
str responseBody;
{
// Create the authentication client
str accessToken = program.getAccessTokenAsync(tokenEndPoint, clientId, clientSecret);
{
error(/Access token retrieval failed./);
return;
}
headers.Add(/Authorization/, /Bearer / + accessToken);
str apiUrl = /URI/; // Replace with your API endpoint URL
// Create the HTTP request
request = System.Net.WebRequest::Create(apiUrl) as System.Net.HttpWebRequest;
//request.set_Headers(headers);
request.Method = 'POST';
JSONContract contract = new JSONContract();
contract.parmBOMId(/string/);
contract.parmCustRef(/string/);
//This is a class for handling JSON
JsonParser jsonParser = new JsonParser();
str jsonBody = jsonParser.objtoJson(contract);
{
streamWriter.Write(jsonBody);
streamWriter.Flush();
}
response = request.GetResponse();
responseBody = response.GetResponseStream().ToString();
//streamRead = new System.IO.StreamReader(response.GetResponseStream());
info(/jsonBody: / + jsonBody);
}
catch (Exception::Error)
{
error(/An error occurred during authentication or the HTTP request: /); //+ errorTextGet());
}
}
I will be very thankful to you.