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