如何从 Dynamics 365 读取 Web api 调用的结果?

How can I read result of web api call from Dynamics 365?

我尝试从 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 对象的结果。

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

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"];