使用客户端密码访问 Dynamics Business Central
Access to Dynamics Business Central with Client Secret
我正在尝试使用身份验证详细信息连接到 Business Central 服务,但出现错误。而当我在 Postman 上测试它时,它运行良好。
我一定是遗漏了一些我没有看到的东西,希望你能帮助我。
到目前为止我的代码如下:
using System.Net.Http;
using System.Net.Http.Headers;
using Microsoft.Identity.Client;
private void BusinessCentral()
{
string TenantId = "...";
string ClientId = "...";
string ClientSecret = "...";
string CallbackUrl = @"https://businesscentral.dynamics.com/";
string GetUrl = string.Format(@"https://api.businesscentral.dynamics.com/v2.0/{0}/Production/api/v2.0/companies", TenantId);
try
{
string[] Scopes = new[] { "https://api.businesscentral.dynamics.com/.default" };
var App = ConfidentialClientApplicationBuilder
.Create(ClientId)
.WithClientSecret(ClientSecret)
.WithRedirectUri(CallbackUrl)
.WithTenantId(TenantId)
.Build();
var result = App.AcquireTokenForClient(Scopes).ExecuteAsync().Result;
if (result != null)
{
HttpClient HttpCli = new HttpClient();
var defaultRequestHeaders = HttpCli.DefaultRequestHeaders;
if (defaultRequestHeaders.Accept == null || !defaultRequestHeaders.Accept.Any(m => m.MediaType == "application/json"))
{
HttpCli.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
defaultRequestHeaders.Add("Authorization", result.CreateAuthorizationHeader());
HttpResponseMessage response = HttpCli.GetAsync(GetUrl).Result;
if (response.IsSuccessStatusCode)
{
string json = response.Content.ReadAsStringAsync().Result;
var apiResult = JsonConvert.DeserializeObject<List<JObject>>(json);
}
else
{
string content = response.Content.ReadAsStringAsync().Result;
// ERROR: "code": "Authentication_InvalidCredentials", "message":"The server has rejected the client credentials.
}
}
}
catch (Exception ex)
{
}
}
它 returns 的错误如下:
{"error":{"code":"Authentication_InvalidCredentials","message":"The server has rejected the client credentials. CorrelationId: 161ce9f5-f2f7-4f56-ad1e-b5a7a159292e."}}
推荐link:Github: active-directory-dotnetcore-daemon-v2
感谢您的支持。
代码正确,问题出在与权限相关的 Business Central 配置上。
我无权访问该配置,因此无法提供详细信息。
我正在尝试使用身份验证详细信息连接到 Business Central 服务,但出现错误。而当我在 Postman 上测试它时,它运行良好。
我一定是遗漏了一些我没有看到的东西,希望你能帮助我。 到目前为止我的代码如下:
using System.Net.Http;
using System.Net.Http.Headers;
using Microsoft.Identity.Client;
private void BusinessCentral()
{
string TenantId = "...";
string ClientId = "...";
string ClientSecret = "...";
string CallbackUrl = @"https://businesscentral.dynamics.com/";
string GetUrl = string.Format(@"https://api.businesscentral.dynamics.com/v2.0/{0}/Production/api/v2.0/companies", TenantId);
try
{
string[] Scopes = new[] { "https://api.businesscentral.dynamics.com/.default" };
var App = ConfidentialClientApplicationBuilder
.Create(ClientId)
.WithClientSecret(ClientSecret)
.WithRedirectUri(CallbackUrl)
.WithTenantId(TenantId)
.Build();
var result = App.AcquireTokenForClient(Scopes).ExecuteAsync().Result;
if (result != null)
{
HttpClient HttpCli = new HttpClient();
var defaultRequestHeaders = HttpCli.DefaultRequestHeaders;
if (defaultRequestHeaders.Accept == null || !defaultRequestHeaders.Accept.Any(m => m.MediaType == "application/json"))
{
HttpCli.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
defaultRequestHeaders.Add("Authorization", result.CreateAuthorizationHeader());
HttpResponseMessage response = HttpCli.GetAsync(GetUrl).Result;
if (response.IsSuccessStatusCode)
{
string json = response.Content.ReadAsStringAsync().Result;
var apiResult = JsonConvert.DeserializeObject<List<JObject>>(json);
}
else
{
string content = response.Content.ReadAsStringAsync().Result;
// ERROR: "code": "Authentication_InvalidCredentials", "message":"The server has rejected the client credentials.
}
}
}
catch (Exception ex)
{
}
}
它 returns 的错误如下:
{"error":{"code":"Authentication_InvalidCredentials","message":"The server has rejected the client credentials. CorrelationId: 161ce9f5-f2f7-4f56-ad1e-b5a7a159292e."}}
推荐link:Github: active-directory-dotnetcore-daemon-v2
感谢您的支持。
代码正确,问题出在与权限相关的 Business Central 配置上。
我无权访问该配置,因此无法提供详细信息。