【发布时间】:2019-03-07 13:50:29
【问题描述】:
我正在使用以下 Web api 从 Dynamics CRM 中查询数据
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
HttpClient client = new HttpClient(new HttpClientHandler() { Credentials = new NetworkCredential("xxxxx@mydomain.com", "!@Demo1$#2") });
client.BaseAddress = new Uri("https://xxxxx.crm.dynamics.com");
client.Timeout = new TimeSpan(0, 2, 0);
string contactAltUri = client.BaseAddress + "api/data/v9.0/accounts?$select=name&$top=3";
HttpResponseMessage createResponseAlt1 = await client.GetAsync(contactAltUri);
现在在对象createResponseAlt1 中,我可以看到以下未经授权的错误。查询数据并将数据保存到 Dynamics CRM 的正确方法是什么?我有用户名、密码和 Ms crm 子域 url。
{StatusCode: 401, ReasonPhrase: 'Unauthorized', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
x-ms-service-request-id: 4b323404-cc13-407c-bb6d-56b61823ab84
REQ_ID: 4b323404-cc13-407c-bb6d-56b61823ab84
AuthActivityId: 85696c96-6803-4664-9391-d28f45d1766a
NativeWebSession-Version: 2
Date: Thu, 07 Mar 2019 13:42:36 GMT
Set-Cookie: ApplicationGatewayAffinity=03da1c2a15fe28b54ffa99b7eab01d12cd9b55dfb1779b6cccce0809ec64f39a;Path=/;Domain=xxxxx.crm.dynamics.com
Server:
WWW-Authenticate: Bearer authorization_uri=https://login.microsoftonline.com/e20b0aa3-6ec3-4272-a76a-aaa32e0f10d6/oauth2/authorize, resource_id=https://xxxxx.crm.dynamics.com/
Content-Length: 0
}}
【问题讨论】:
-
所使用的登录帐户应具有在 CRM 中分配的安全角色。可以在浏览器中登录吗?
-
是的,我可以在浏览器中登录
-
复制你在contactAltUti变量中得到的值并粘贴到浏览器地址栏中,看看它是否有效?
-
您如何将 Rest Builder github.com/jlattimer/CRMRESTBuilder 添加到您的 CRM 组织中,然后尝试您的 Webapi 查询。我不确定使用 Webapi 是否是您查询/更新 CRM 的唯一必需方法。如果不能,我可以建议另一种方式,使用组织服务。这是参考arunpotti.wordpress.com/2018/02/03/… 的示例链接
标签: c# dynamics-crm dynamics-crm-online dynamics-365