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...
Unanswered

To get access token method with oauth2 for Business Central through c#

(0) ShareShare
ReportReport
Posted on by 10

Does anyone have working example of windows C# code that successfully generate the access token and read the data's from the Business Central through OAuth2 authentication.

I have successfully retrieved the token from Postman but when I try to get the token from my C# code I can't get the correct token. I get wrong one. 

Here's the code I used to get the access token:

public string GetAccessToken()

{

       var tenantId = "27d1c216-24…";

       var clientId = "877edd8a-5a…";

       var authorityUri = $"https://login.microsoftonline.com/{tenantId}";

       var redirectUri = "http://localhost";

var scopes = new List { "https://api.businesscentral.dynamics.com/.default" };

       var clientSecret = "S1h…";

       var confidentialClient = ConfidentialClientApplicationBuilder

              .Create(clientId)

              .WithClientSecret(clientSecret)

              .WithAuthority(new Uri(authorityUri))

              .WithRedirectUri(redirectUri)

              .Build();

       var accessTokenRequest = confidentialClient.AcquireTokenForClient(scopes);

       var accessToken = accessTokenRequest.ExecuteAsync().Result.AccessToken;

       return accessToken;

}

In the above code I used "ConfidentialClientApplicationBuilder" to get the access token. I get the token, but its the wrong one.  And also I try to get the token through "PublicClientApplicationBuilder" but its not working.

Also wants to clarify this,
- Is the above GetAccessToken code only retrieve to view the environment list and not allow to read data/update data?

- Is using OAuth 2.0 to read/update data service to service is currently not available in Business Central?

Thanks

I have the same question (0)
  • Techworld Profile Picture
    106 on at

    https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/administration/automation-apis-using-s2s-authentication

    I will recommend this if you are using Cloud and upgraded 18.3.

    It is easy to configure and work. 

    and there is a sample attached for c# project on Microsoft document, refer this

    https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/webservices/authenticate-web-services-using-oauth#create-the-c-project

  • Raja Priya Profile Picture
    10 on at

    Thanks for your reply.

    I also referred the sample attached for c# project on Microsoft document. I got an error in the below line,

    var nav = new NAV.NAV(new Uri(<"localhost:7048/.../Company('CRONUS International Ltd.'>)"));

    In this line I got an error with the NAV.NAV

    I need to read the business central purchase order data's in my application. I have successfully retrieved the token from Postman but when I try to get the token from my C# code I can't get the correct token. But I generated the token from the postman and pass that token as manually in my application, its works and retrieved the purchase order's data from the business central.

    I need to generate the access token in my windows c# application. Please guide me how to overcome with it.

    Thanks a lot.

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 3,229

#2
Jainam M. Kothari Profile Picture

Jainam M. Kothari 1,867 Super User 2025 Season 2

#3
YUN ZHU Profile Picture

YUN ZHU 1,153 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans