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

Faran123
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();
//___________________________________________________________________________________________________________________________________________________
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();
//___________________________________________________________________________________________________________________________________________________
0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions