Hi,
I am trying to implement Oauth logic for Business Central 365 via this code
private const string ClientId = "XXXXXXXXXX";
private const string ClientSecret = "XXXXXXXXXXX";
private const string AadTenantId = "XXXXXXXXXXX";
private const string Authority = "">login.microsoftonline.com/.../token"; // I do not know why, but dynamics forum does not allow me to write this here : // https ://login.microsoftonline.com/{AadTenantId}/oauth2/v2.0/token
private const string BCEnvironmentName = "XXXXXXXX";
private const string BCCompanyId = "XXXXXXXXXXX";
private const string BCBaseUrl = "">api.businesscentral.dynamics.com/.../companies({BCCompanyId}))"; // I do not know why, but dynamics forum does not allow me to write this here : https ://api.businesscentral.dynamics.com/v2.0/{BCEnvironmentName}/api/v2.0/companies({BCCompanyId})
private static AuthenticationResult AuthResult = null;
static void Main(string[] args)
{
string customers = CallBusinessCentralAPI(BCEnvironmentName, BCCompanyId, "customers").Result;
}
static async Task<AuthenticationResult> GetAccessToken(string aadTenantId)
{
Uri uri = new Uri(Authority.Replace("{AadTenantId}", aadTenantId));
IConfidentialClientApplication app = ConfidentialClientApplicationBuilder.Create(ClientId)
.WithClientSecret(ClientSecret)
.WithAuthority(uri)
.Build();
string[] scopes = new string[] { @"">api.businesscentral.dynamics.com/.default" }; // I do not know why, but dynamics forum does not allow me to write this here : https: //api.businesscentral.dynamics.com/.default
AuthenticationResult result = null;
try
{
result = await app.AcquireTokenForClient(scopes).ExecuteAsync();
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Token acquired");
Console.ResetColor();
}
catch (MsalServiceException ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Error occurred while retrieving access token");
Console.WriteLine($"{ex.ErrorCode} {ex.Message}");
Console.ResetColor();
}
return result;
}
static async Task<string> CallBusinessCentralAPI(string bcEnvironmentName, string bcCompanyId, string resource)
{
string result = string.Empty;
if ((AuthResult == null) || (AuthResult.ExpiresOn < DateTime.Now))
{
AuthResult = await GetAccessToken(AadTenantId);
}
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AuthResult.AccessToken);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
resource = "purchaseInvoices";
Uri uri = new Uri(GetBCAPIUrl(bcEnvironmentName, bcCompanyId, resource));
HttpResponseMessage response = await client.GetAsync(uri);
if (response.IsSuccessStatusCode)
{
result = await response.Content.ReadAsStringAsync();
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Call to Business Central API failed: {response.StatusCode} {response.ReasonPhrase}");
string content = await response.Content.ReadAsStringAsync();
Console.WriteLine($"Content: {content}");
Console.ResetColor();
}
}
return result;
}
private static string GetBCAPIUrl(string bcEnvironmentName, string bcCompanyId, string resource)
{
return BCBaseUrl.Replace("{BCEnvironmentName}", bcEnvironmentName).Replace("{BCCompanyId}", bcCompanyId) + "/" + resource;
}
The token is successfully generated, so there is anything wrong that I have done during configuration at Azure Active Directory
The error that I receive during GET request is 401 Error

I followed instructions according to official documentation how to form URL - learn.microsoft.com/.../endpoints-apis-for-dynamics

But it did not help
That is how I am trying to from URl currently

What is wrong with my code ?
Thanks in advance
Kindest regards