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
  • KT-04081410-0 Profile Picture
    on at
    businesscentral.dynamics
    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
     
  • CU26071644-0 Profile Picture
    on at
    businesscentral.dynamics
    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?
  • Pradeep Rai Profile Picture
    5,482 Super User 2025 Season 1 on at
    businesscentral.dynamics
    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.
  • KT-04081410-0 Profile Picture
    on at
    businesscentral.dynamics
    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?
  • Suggested answer
    Pradeep Rai Profile Picture
    5,482 Super User 2025 Season 1 on at
    businesscentral.dynamics
    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;
                }
            }
        }
    }
    
     

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,886 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,768 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans