如何使用Netcore API获取Microsoft Dynamics 365 V9的OAuth访问令牌

时间:2019-05-23 22:31:05

标签: asp.net-core oauth-2.0 asp.net-identity dynamics-365

我有一个试图连接到Microsoft Dynamics 365实例的Aspnet Core API。目的是使用API​​将数据推送到CRM实例。

为此,我有一个CRM用户和一个通行证,我的理解是我将需要一个OAuth令牌来实际完成我的请求。 我该如何从API获取该OAuth令牌?

我当前(无效)的代码如下:

  using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(CrmFInanceCCNotifyDto.Instance.CrmBaseUri);
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
                "Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes($"{CrmFInanceCCNotifyDto.Instance.CrmUser}:" +
                $"{CrmFInanceCCNotifyDto.Instance.CrmPass}")));
                client.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0");
                client.DefaultRequestHeaders.Add("OData-Version", "4.0");
                client.DefaultRequestHeaders.Add("Accept", "application/json");
                client.DefaultRequestHeaders.Add("If-Match", "*");
                var method = "PATCH";
                var httpVerb = new HttpMethod(method);
                string path = client.BaseAddress + CrmFInanceCCNotifyDto.Instance.CrmLocationUpdateUri;
                string jsonPayload = "{\"entity.dfnd_financingused\": 1}";
                var stringContent = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
                var httpRequestMessage =
                    new HttpRequestMessage(httpVerb, path)
                    {
                        Content = stringContent
                    };
                try
                {
                    var response = await client.SendAsync(httpRequestMessage);

任何解决此问题的指针将不胜感激。

谢谢

0 个答案:

没有答案