I am getting status 400 error while creating record in Crm its post method, for get method its working fine, in crm restbuilder its working fine to create record.
string resource = "https://*****.crm4.dynamics.com/";
UserCredential clientredentials = new UserCredential("****t@******onmicrosoft.com", "****@716");
string client_id = "2b572be0-*****-4581-8481-*******";
AuthenticationContext authenticationContext = new AuthenticationContext("">login.microsoftonline.com/.../authorize");
AuthenticationResult authenticationResult = authenticationContext.AcquireToken(resource, client_id, clientredentials);
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0");
httpClient.DefaultRequestHeaders.Add("OData-Version", "4.0");
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authenticationResult.AccessToken);
httpClient.BaseAddress = new Uri("">orgf65ea8ad.api.crm4.dynamics.com/.../");
account_creation_1 ac_1 = new account_creation_1()
{
name = account_Creation.name,
preetham_reasonforregistration = account_Creation.reason_for_registration,
preetham_DepartmentId = Guid.Parse(account_Creation.Department),
preetham_doctorId = Guid.Parse(account_Creation.Doctor)
};
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post,new Uri("https://*****.api.crm4.dynamics.com/api/data/v9.2/accounts"))
{
Content = new StringContent(ac_1.ToString(), Encoding.UTF8, "application/json")
};
var response = httpClient.SendAsync(request, HttpCompletionOption.ResponseContentRead).Result;
if (response.IsSuccessStatusCode)
{
Console.Write("Account Created");
}
}
I am getting the access token its everything working fine, anybody have any idea where i am wrong