簡體   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