The code below is what I have written to hit a Identity service to get the access token as the response(json response). Its a post with the included request body. The request is expected in json which I hope I'm sending in the same way. I have tried this request with the same body in fiddler and it works fine.
please help me out identify the mistake.
public string GetClientIDFromIdentityService()
{
loggingService.Debug("GetClientIDFromIdentityService started");
string url = strong textConfigurationManager.AppSettings["IdentityServiceEndPoint"].ToString();
string IdentityServiceResponse = null;
IdentityServiceRequest request = new IdentityServiceRequest();
request.client_id = ConfigurationManager.AppSettings["ClientID"];
request.grant_type = ConfigurationManager.AppSettings["GrantType"];
request.scope = ConfigurationManager.AppSettings["Scope"];
request.username = ConfigurationManager.AppSettings["UserName"];
request.password = ConfigurationManager.AppSettings["Password"];
string accessToken = "";
try
{
using (var _client = new HttpClient())
{
var javaScriptSerializer = new JavaScriptSerializer();
string jsonString = javaScriptSerializer.Serialize(request);
HttpContent content = new StringContent(jsonString);
_client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var getTask = _client.PostAsync(url, content).ContinueWith((taskWithResponse) =>
{
HttpResponseMessage response = new HttpResponseMessage();
response = taskWithResponse.Result;
var readTask = response.Content.ReadAsStringAsync();
readTask.Wait();
IdentityServiceResponse = readTask.Result;
});
getTask.Wait();
JObject obj = JObject.Parse(IdentityServiceResponse);
accessToken = obj["access_token"].ToString();
}
}
catch (Exception ex)
{
loggingService.Error("Caught error at GetClientIDFromIdentityService Start", ex);
}
loggingService.Debug("GetClientIDFromIdentityService ended" + " AccessToken : " + accessToken);
return accessToken;
}