动态WEB API在post -StatusCode上返回:401,ReasonPhrase:'Unauthorized'

时间:2017-02-02 07:53:19

标签: asp.net-web-api http-post dynamics-crm-2011 microsoft-dynamics dynamics-crm-2013

我正在尝试通过 web api 创建 CRM 的领导者,但是它会导致未经授权的访问错误。我能够登录CRM,但是从Web Api,它正在抛出错误 以下是我的代码

var credentials = new NetworkCredential("username", "password");
        var client = new HttpClient(new HttpClientHandler() { Credentials = credentials })
        {
            BaseAddress = new Uri("https://*******.dynamics.com/XRMServices/2011/Organization.svc/api/data/v8.2/")
        };
 Entity lead1 = new Entity();
           lead1["firstname"] = "TestFirstName";
           lead1["lastname"] = "TestLastName";
           lead1["emailaddress1"] = "%%%%%%%%%";
           lead1["companyname"] = "&&&&&&";



            string output = new JavaScriptSerializer().Serialize(lead1).ToString();

            HttpRequestMessage request = null;

            try
            {

            request = new HttpRequestMessage(HttpMethod.Post, "leads");
            request.Content = new StringContent(output); 
            request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
            Task<HttpResponseMessage> response = client.SendAsync(request);

            response.Wait();       
            if (response.Result.IsSuccessStatusCode)
            {
                //retrievedContact1 = JsonConvert.DeserializeObject<JObject>(rep    .Content.ReadAsStringAsync());
            }

0 个答案:

没有答案