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 :
Customer experience | Sales, Customer Insights,...
Suggested Answer

businesscentral.dynamics

(0) ShareShare
ReportReport
Posted on by
My main question is I'm in the process of creating a 3rd party app and trying to request an access token using a http request. I went thru Azure and created an APP and got a client_id and client_secret. I first tried this in Postman using https://businesscentral.dynamics.com/ as the URL and it worked returning an access token.
 
I then create a small Visual Studio C# and re-tried (local debug) using the same setup as in Postman.
 
My http request keeps returning a 'The remote server returned an error: (401) Unauthorized'
so my thought is that I need to authorize the 3rd party app before it lets me get an access token.
 
I assume Postman is already authorized so that's why it works.
 
Can someone tell me if I'm correct and what my next step is.
 
Thanks Keith
I have the same question (0)
  • Suggested answer
    Pradeep Rai Profile Picture
    5,489 Moderator on at
    Hi,

    try below code to generate access token:
     
    
    
    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;
                }
            }
        }
    }
    
     
  • KT-04081410-0 Profile Picture
    on at
    Thanks for the reply
     
    One other question I only have a free account at the moment do I need to get a pay account before getting the ability to get an access token and read various entities with the web api?
  • Pradeep Rai Profile Picture
    5,489 Moderator on at
    You need to try but based on my searching it seems that you can access the API no need to purchase.

    Thank You,
    Pradeep rai.
  • CU26071644-0 Profile Picture
    on at
    Thanks for the reply..
     
    I still get the (401) Unauthorized , so my next question is
     
    Do I need to add or change any setting or privileges in Azure to run a Web API?
  • KT-04081410-0 Profile Picture
    on at
    I am a Global Administrator but when I tried to authorize API's to read-write-all for Dynamics ERP,Dynamics 365 Business Central I get the following error message, since I just have a trial subscription, is this message caused by the trial subscription or possibly something else?
     
    Message:
    Could not grant admin consent. Your organization does not have a subscription (or service principal) for the following API(s): Dynamics ERP,Dynamics 365 Business Central
     
    What should I do next?
     
    Thanks
    Keith
     

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 > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 70 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 33 Most Valuable Professional

#3
Daniyal Khaleel Profile Picture

Daniyal Khaleel 32 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans