Options

Connect to BC web service from a windows console application using Oauth 2 for user authorization

Faran123Faran123 Member Posts: 11
Hi community,
I am using following code to access business central web service using Oauth2 authentication from C# console application, the access token generated successfully but I'm unbale to access BC web service due to invalid credentials, problem is that the access token generated from below code is rather shorter (1500) characters but from postman it is around (1700) characters.
Can anyone help me in this regard. I copied the following code from following link:

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

//___________________________________________________________________________________________________________________________________________________
static void Main(string[] args)
{
// Get access token from Azure AD. This will show the login dialog.
var authenticationContext = new AuthenticationContext("https://login.microsoftonline.com/"+AadTenantId, false);
AuthenticationResult authenticationResult = authenticationContext.AcquireTokenAsync(ServerAppIdUri, ClientId, new Uri(ClientRedirectUrl), new PlatformParameters(PromptBehavior.SelectAccount)).GetAwaiter().GetResult();
// Connect to the Business Central OData web service
var nav = new NAV.NAV(new Uri("https://api.businesscentral.dynamics.com/v2.0/43af5a82-9209-4329-8234-d253b1905206/BCTEST/ODataV3/Company('CRONUS Danmark A/S')"));
nav.BuildingRequest += (sender, eventArgs) => eventArgs.Headers.Add("Authorization", authenticationResult.CreateAuthorizationHeader());

// Retrieve and return a list of the customers
foreach (var customer in nav.AccountantPortalActivityCues)
{
Console.WriteLine("Found customer: " + customer.CurrencySymbol);
}
Console.ReadLine();
//___________________________________________________________________________________________________________________________________________________
Sign In or Register to comment.