Reputation: 185
I have a demo tenant with Dynamics Business Central and I am working with OData to make CRUD examples for Company entity and after it works for Invoice, Quotes, Orders, Products and others. I have downloaded the Metadata file using this URL call: https://api.businesscentral.dynamics.com/v2.0/{tenantid}/Production/ODataV4/$metadata
The access token is provided using IConfidentialClientApplication and AuthenticationResult. The token is correct and I can get the List of companies from Business Central when using it in postman.
I am passing the token in every odata call like this:
myNAV.NAV bcContext = new myNAV.NAV(new Uri("https://api.businesscentral.dynamics.com/v1.0/api/beta/"));
bcContext.SendingRequest2 += new EventHandler<SendingRequest2EventArgs>((sender, e) => oDataExtension.OnSendingRequest(sender, e, crmAuthResult.AccessToken));
public class oDataExtension
{
public const string POSTLocationHeaderException = "The response to this POST request did not contain a 'location' header. That is not supported by this client.";
public static void OnSendingRequest(object sender, SendingRequest2EventArgs e, string token)
{
// Add an Authorization header that contains an OAuth WRAP access token to the request.
e.RequestMessage.SetHeader("Authorization", token);
//if (e.RequestMessage.Method.Equals("PUT", System.StringComparison.InvariantCultureIgnoreCase))
//{
// //e.RequestMessage.Method = "PATCH";
//}
if (!e.RequestMessage.Method.Equals("POST", System.StringComparison.InvariantCultureIgnoreCase) && !e.RequestMessage.Method.Equals("PATCH", System.StringComparison.InvariantCultureIgnoreCase))
{
e.RequestMessage.SetHeader("Prefer", "odata.include-annotations=\"*\"");
}
e.RequestMessage.SetHeader("accept-language", "en-US,en;q=0.9,hr;q=0.8");
}
}
This is the code of my GET Company OData call:
List<myNAV.Company> customQuotes = (await ((DataServiceQuery<myNAV.Company>)bcContext.Company).ExecuteAsync()).ToList();
the URL generated by the code looks like this: https://api.businesscentral.dynamics.com/v1.0/api/beta/company
but the correct URL should look like this: https://api.businesscentral.dynamics.com/v1.0/api/beta/companies
Postman returns Error 404 with this url: https://api.businesscentral.dynamics.com/v1.0/api/beta/company, but the same call in the DataServiceQuery return Error 401 the credentials are incorrect.
After getting the Metadata file from a different url(https://api.businesscentral.dynamics.com/v1.0/api/beta$metadata#) the query generates the correct url but I still get the error that I use incorrect credentials but when I use the URL and Access Token in postman it return 200 Success with correct company values.
Upvotes: 0
Views: 245
Reputation: 185
The AcquireTokenOnBehalfOf method for getting AuthenticationResult does not return the word 'Bearer' with the Access Token in Business Central, the word 'Bearer' is returned when you are acquiring token for CRM Customer Service so I did not think there would be problems there.
Upvotes: 0