Hi thanks a lot for your answer, yes
My steps:
-
I created an app registration on Azure portal
1.1. I added Business Central as API permission
1.2. Created the secret key for that App registration.
-
create a console application to get token:
using System;
using System.Threading.Tasks;
using System.Net.Http;
using System.Net.Http.Headers;
using RestSharp;
using Newtonsoft.Json;
using AuthenticationContext = Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
namespace BCTestConnectApi
{
class BCAPIReq
{
static string resource = "https://api.businesscentral.dynamics.com";
static string authorityUri = "https://login.microsoftonline.com/e53d2a55-6254-40d3-bf7e-6cb554a2s285";
static string clientId = "d7f6b8e1-b3a0-4454-c4bb-fb8de6a3e2c5";
static string clientSecret = "_9XPgsLAvoRNQR?WzbSASrDMjx4c6G82";
static string accessToken = null;
static string bcResponse;
static void Main(string[] args)
{
ClientCredential clientCredential = new ClientCredential(clientId, clientSecret);
var authenticationContext = new AuthenticationContext(authorityUriv2, true);
AuthenticationResult authenticationResult = authenticationContext.AcquireTokenAsync(resource, clientCredential).GetAwaiter().GetResult();
accessToken = authenticationResult.AccessToken;
var clientBC = new RestClient("">api.businesscentral.dynamics.com/.../companies");
clientBC.Timeout = -1;
var requestBC = new RestRequest(Method.GET);
requestBC.AddHeader("Content-Type", "application/x-www-form-urlencoded");
requestBC.AddHeader("Authorization", "Bearer " + accessToken);
requestBC.AddHeader("Accept", "*/*");
requestBC.AddHeader("Cache-Control", "no-cache");
requestBC.AddHeader("Host", "api.businesscentral.dynamics.com");
requestBC.AddHeader("Accept-Encoding", "gzip, deflate, br");
requestBC.AddHeader("Connection", "keep-alive");
requestBC.AddParameter("application/x-www-form-urlencoded", "", ParameterType.RequestBody);
IRestResponse responseBC = clientBC.Execute(requestBC);
bcResponse = responseBC.Content;
object resposeDataBC = JsonConvert.DeserializeObject(bcResponse);
Console.Write(resposeDataBC);
Console.ReadKey();
}
}
But I received that credentials are not vaild, my app registration secret, are ok because I tested with postman and works, however here in my console is not working.
Thanks a lot in advance for your help