下面的代码是我编写的用于访问Identity服务以获取访问令牌作为响应(json响应)的代码。它是一个包含请求正文的帖子。 请求在json中,我希望我以相同的方式发送。 我在fiddler中用同一个身体尝试过这个请求,它运行正常。
请帮我识别错误。
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;
}