namespace CRM.Pradeep
{
public class BusinessCentralCRM: IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
// Obtain the tracing service
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
// Authenticate with Business Central
Task<string> accessTokenTask = AuthenticateWithBusinessCentral(tracingService);
accessTokenTask.Wait();
string accessToken = accessTokenTask.Result;
}
private async Task<string> AuthenticateWithBusinessCentral(ITracingService tracingService)
{
try
{
string tenantId = "your_tenant_id";
string clientId = "your_client_id";
string clientSecret = "your_client_secret";
string resource = "https://api.businesscentral.dynamics.com";
string tokenEndpoint = $"https://login.microsoftonline.com/{tenantId}/oauth2/token";
using (HttpClient client = new HttpClient())
{
var content = new StringContent($"grant_type=client_credentials&client_id={clientId}&client_secret={Uri.EscapeDataString(clientSecret)}&resource={Uri.EscapeDataString(resource)}", Encoding.UTF8, "application/x-www-form-urlencoded");
HttpResponseMessage response = await client.PostAsync(tokenEndpoint, content);
if (response.IsSuccessStatusCode)
{
string responseContent = await response.Content.ReadAsStringAsync();
return responseContent;
}
else
{
tracingService.Trace($"Error: {response.StatusCode}");
return null;
}
}
}
catch (Exception ex)
{
tracingService.Trace($"Exception: {ex.Message}");
return null;
}
}
}
}
André Arnaud de Cal...
292,886
Super User 2025 Season 1
Martin Dráb
231,768
Most Valuable Professional
nmaenpaa
101,156
Moderator