Skip to main content

Notifications

Announcements

No record found.

Small and medium business | Business Central, N...
Unanswered

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

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

  • Raja Priya Profile Picture
    Raja Priya 10 on at
    RE: To get access token method with oauth2 for Business Central through c#

    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.

  • Techworld Profile Picture
    Techworld 106 on at
    RE: To get access token method with oauth2 for Business Central through c#

    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

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans