繁体   English   中英

使用客户端密码访问 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)
    {
        
    }
}

它返回的错误如下:

{"error":{"code":"Authentication_InvalidCredentials","message":"服务器已拒绝客户端凭据。CorrelationId: 161ce9f5-f2f7-4f56-ad1e-b5a7a159292e。"}}

推荐链接: Github:active-directory-do.netcore-daemon-v2

感谢您的支持。

代码是正确的,问题出在与权限相关的 Business Central 配置上。

我无权访问该配置,因此无法提供详细信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM