{
try
{
string bearerToken = "";
string ClientId = "(removed)";
string ClientSecret = "(removed)";
string TenantId = "(removed)";
var content = new StringContent("grant_type=client_credentials" +
"&scope=https://api.businesscentral.dynamics.com/.default" +
"&client_id=" + HttpUtility.UrlEncode(ClientId) +
"&client_secret=" + HttpUtility.UrlEncode(ClientSecret));
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/x-www-form-urlencoded");
var response = await client.PostAsync(tokenUrl, content);
{
var tokenContent = await response.Content.ReadAsStringAsync();
var tokenResponse = Newtonsoft.Json.JsonConvert.DeserializeObject<TokenResponse>(tokenContent);
bearerToken = tokenResponse.AccessToken;
string apiUrl = $"https://api.businesscentral.dynamics.com/v2.0/(removed)/Sandbox/api/v2.0/companies((removed))/customers";
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + bearerToken);
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var apiRespponse = await httpClient.GetAsync(apiUrl);
if (apiRespponse.IsSuccessStatusCode)
{
string resultString = await httpClient.GetStringAsync(apiUrl);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
public class TokenResponse
{
[Newtonsoft.Json.JsonProperty("access_token")]
public string AccessToken { get; set; }
}