[英]How to request Web API OAuth token using HttpClient in a C# WPF?
I'm new in WPF and want to call oauth token api to get authenticated token through HttpClient.我是 WPF 的新手,想调用 oauth 令牌 api 以通过 HttpClient 获取经过身份验证的令牌。 I have tried many solutions but still getting response Bad request(400).
我尝试了很多解决方案,但仍然收到错误请求(400)的响应。
I have checked with postman as well, api works fine.我也检查了 postman,api 工作正常。
postman response postman 响应
Tried Solutions:尝试过的解决方案:
(1) (1)
var request = new HttpRequestMessage(HttpMethod.Post, url);
request.Content = new FormUrlEncodedContent(param);
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
request.Content.Headers.ContentType.CharSet = "UTF-8";
using (HttpResponseMessage response = await ApiHelper.apiClient.SendAsync(request))
{
Console.WriteLine("postRequest -> StatusCode -> " + response.StatusCode);
if (response.IsSuccessStatusCode)
{
//ComicModel comic = await response.Content.ReadAsAsync<ComicModel>();
var responseString = await response.Content.ReadAsStringAsync();
return response;
}
else
{
Console.WriteLine("postRequest -> exception -> " + response);
throw new Exception(response.ReasonPhrase);
}
}
(2) (2)
using (var content = new FormUrlEncodedContent(param))
{
content.Headers.Clear();
content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
using (HttpResponseMessage response = await ApiHelper.apiClient.PostAsync(url, content))
{
Console.WriteLine("postRequest -> StatusCode -> " + response.StatusCode);
if (response.IsSuccessStatusCode)
{
//ComicModel comic = await response.Content.ReadAsAsync<ComicModel>();
var responseString = await response.Content.ReadAsStringAsync();
return response;
}
else
{
Console.WriteLine("postRequest -> exception -> " + response);
throw new Exception(response.ReasonPhrase);
}
}
}
(3) (3)
using (HttpResponseMessage response = await ApiHelper.apiClient.PostAsync(url, new FormUrlEncodedContent(param)))
{
Console.WriteLine("postRequest -> StatusCode -> " + response.StatusCode);
if (response.IsSuccessStatusCode)
{
//ComicModel comic = await response.Content.ReadAsAsync<ComicModel>();
var responseString = await response.Content.ReadAsStringAsync();
return response;
}
else
{
Console.WriteLine("postRequest -> exception -> " + response);
throw new Exception(response.ReasonPhrase);
}
}
All solutions respond me 400 bad request.所有解决方案都响应我 400 错误请求。
My params are:我的参数是:
var params= new List<KeyValuePair<string, string>>();
params.Add(new KeyValuePair<string, string>("grant_type", "password"));
params.Add(new KeyValuePair<string, string>("username", HttpUtility.UrlEncode("myUsername")));
params.Add(new KeyValuePair<string, string>("password", HttpUtility.UrlEncode("myPass")));
How can I fix it?我该如何解决? Thank you in advance.
先感谢您。
How do you authenticate with Postman?您如何使用 Postman 进行身份验证? I do not see it in your code.
我在您的代码中没有看到它。 It depends on how the api is configured.
这取决于 api 的配置方式。 Be sure to use the appropriate auth method in your client call, for example Bearer token.
请务必在您的客户端调用中使用适当的身份验证方法,例如 Bearer 令牌。 Api should return 400 instead of 401.
Api 应该返回 400 而不是 401。
httpClient.SetBearerToken(accessToken);
Oops.哎呀。 my base url starts with http.
我的基础 url 以 http 开头。 I forgot to replace it with https.
我忘了用 https 替换它。 Everything is fine with https: :)
https 一切都很好::)
Thanks谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.