0

我尝试从 Dynamics 365 Sales 中检索记录。我在 Azure 中创建了一个应用注册,我可以根据这个应用获取令牌。

另外,我可以调用 HTTP 客户端。但我不知道如何读取 HTTP 调用的结果。

Microsoft 仅发布了WhoAmIRequest示例,但我找不到其他实体的示例。

这是我的示例代码。我尝试阅读身体对象。

try
{
    string serviceUrl = "https://****.crm4.dynamics.com/";
    string clientId = "******";
    string clientSecret = "*******";
    string tenantId = "*******";

    A***.Library.Utility.MSCRM mscrm = new Library.Utility.MSCRM(serviceUrl, clientId, clientSecret, tenantId);
    var token = await mscrm.GetTokenAsync();

    Console.WriteLine(token);

    using (HttpClient client = new HttpClient())
    {
        client.BaseAddress = new Uri(serviceUrl);
        client.Timeout = new TimeSpan(0, 2, 0);  //2 minutes  
        client.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0");
        client.DefaultRequestHeaders.Add("OData-Version", "4.0");
        client.DefaultRequestHeaders.Accept.Add(
                        new MediaTypeWithQualityHeaderValue("application/json"));

        HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "/api/data/v9.0/accounts");

        // Set the access token
        request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);

        HttpResponseMessage response = client.SendAsync(request).Result;

        if (response.IsSuccessStatusCode)
        {
            // Get the response content and parse it.  
            var responseStr = response.Content.ReadAsStringAsync();

            JObject body = JObject.Parse(response.Content.ReadAsStringAsync().Result);
        }
    }
}
catch(Exception e)
{
    Console.WriteLine(e.Message);
}

这是body对象的结果。

在此处输入图像描述

4

1 回答 1

1

您可以使用这些语法中的任何一种来读取值。阅读更多

JObject body = JObject.Parse(response.Content.ReadAsStringAsync().Result);

// Can use either indexer or GetValue method (or a mix of two)
body.GetValue("obs_detailerconfigid");
body["obs_detailerconfigid"];
于 2021-07-11T00:11:54.620 回答