获取HTTP 401使用SOAP UI调用MS CRM 365时出错

时间:2017-01-19 21:39:22

标签: microsoft-dynamics dynamics-crm-365

我正在尝试使用SOAPUI调用MSCRM 365 Web服务,这是我到目前为止所做的

  1. 从我的cRM实例下载组织WSDL
  2. 在SOAPUI上载
  3. 添加了三个标头参数 - Content-Type,SOAPAction和Accept
  4. 在“请求属性”中添加了用户名和密码
  5. 每当我向MSCRM发送请求时,我都会收到“HTTP ERROR 401 - 未经授权:访问被拒绝”

    有人有什么想法吗?

    谢谢, Nitesh

1 个答案:

答案 0 :(得分:0)

由于这是Dynamics 365,因此不会使用用户名/密码进行身份验证。相反,您需要使用OAuth,如链接

中所示

https://msdn.microsoft.com/en-us/library/gg327838.aspx

// TODO Substitute your correct CRM root service address, 
string resource = "https://mydomain.crm.dynamics.com";

// TODO Substitute your app registration values that can be obtained after you
// register the app in Active Directory on the Microsoft Azure portal.
string clientId = "e5cf0024-a66a-4f16-85ce-99ba97a24bb2";
string redirectUrl = "http://localhost/SdkSample";


// Authenticate the registered application with Azure Active Directory.
AuthenticationContext authContext = 
    new AuthenticationContext("https://login.windows.net/common", false);
AuthenticationResult result = authContext.AcquireToken(resource, clientId, new
                                                       Uri(redirectUrl));

在邮件请求中使用访问令牌:

using (HttpClient httpClient = new HttpClient())
{
    httpClient.Timeout = new TimeSpan(0, 2, 0);  // 2 minutes
    httpClient.DefaultRequestHeaders.Authorization = 
        new AuthenticationHeaderValue("Bearer", result.AccessToken);

另一个选择是从Xrm.Client转移到Xrm.Tools.Connection。请参阅此站点中的示例。

https://msdn.microsoft.com/en-us/library/jj602970.aspx