1

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;
    }
  • You don't say what _does_ happen? – sellotape Jan 08 '17 at 13:16
  • 2
    Take a look at the `InnerExceptions` property of the `AggregateException`. And use proper `async` and `await` instead of using `.Result` or `.Wait()`. See http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html. Also, `.Result` or `.Wait()` generate `AggregateException` instead of the underlying actual `Exception`. For final reading see this: http://stackoverflow.com/questions/17284517/is-task-result-the-same-as-getawaiter-getresult – Peter Bons Jan 08 '17 at 13:48
  • thanks for the suggestions.I Could get to the issue by looking through the inner exception . I could find the format of the Object which was receiving the response was not correct. Corrected it and it is working fine now. – ramlath unnisa Jan 09 '17 at 12:53

0 Answers0