web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Suggested Answer

Business Central 365 How to form URL for GET request via C# code

(0) ShareShare
ReportReport
Posted on by 130

Hi,

I am trying to form GET request for retrieving "customers" entity data, but I get different errors such as 'Unauthorized' (401), 'Not found' (404) 

I watched multiple video and read different documentation among which official msdn documentation learn.microsoft.com/.../endpoints-apis-for-dynamics 

pastedimage1668633916348v2.png 

But I still can't achieve workable result

Here is code via which I try to perform my gain

static async Task GetS2SToken()
{
string ClientId = "XXXXXXXXXXX";
string ClientSecret = "XXXXXXXXXXXXXXXX";
string TenantId = "XXXXXXXXXXXXXXXX";

string URL = "">login.microsoftonline.com/" + TenantId + "/oauth2/v2.0/token";

HttpClient client = new HttpClient();
var content = new StringContent("grant_type=client_credentials" +
"&scope=
">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(URL, content);
if (response.IsSuccessStatusCode)
{
JObject Result = JObject.Parse(await response.Content.ReadAsStringAsync());
string BearerToken = Result["access_token"].ToString();

string URL2 = $"">api.businesscentral.dynamics.com/.../customers";
HttpClient testclient = new HttpClient();
testclient.DefaultRequestHeaders.Add("Authorization", "Bearer " + BearerToken);
var result = testclient.GetAsync(URL2).GetAwaiter().GetResult();
if (result.IsSuccessStatusCode)
{
Console.WriteLine(await result.Content.ReadAsStringAsync());
}
else
{
Console.WriteLine(result.StatusCode.ToString());
}

HttpClient testclient2 = new HttpClient();
testclient2.DefaultRequestHeaders.Add("Authorization", "Bearer " + BearerToken);
var result2 = await testclient2.GetAsync(URL2);
if (result2.IsSuccessStatusCode)
{
Console.WriteLine(await result2.Content.ReadAsStringAsync());
}
else
{
Console.WriteLine(result2.StatusCode.ToString());
}


}
else
{
Console.WriteLine(response.StatusCode.ToString());
}
}

Please help me to identify how to form the URL

I have the same question (0)
  • Suggested answer
    YUN ZHU Profile Picture
    95,729 Super User 2025 Season 2 on at

    Hi, It looks like the same problem.

    (+) Business Central 365 c# Oauth implementation - Dynamics 365 Business Central Forum Community Forum

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 2,468

#2
YUN ZHU Profile Picture

YUN ZHU 923 Super User 2025 Season 2

#3
Sumit Singh Profile Picture

Sumit Singh 607

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans