如何在C#WPF中使用HttpClient请求Web API OAuth令牌?
我是WPF新手,希望调用oauth令牌api通过HttpClient获得经过身份验证的令牌。我尝试了许多解决方案,但仍然收到错误请求(400)的响应。如何在C#WPF中使用HttpClient请求Web API OAuth令牌?,c#,wpf,httpclient,access-token,C#,Wpf,Httpclient,Access Token,我是WPF新手,希望调用oauth令牌api通过HttpClient获得经过身份验证的令牌。我尝试了许多解决方案,但仍然收到错误请求(400)的响应。 我也与邮递员核实过,api工作正常。 尝试过的解决方案: (1) var request=newhttprequestmessage(HttpMethod.Post,url); request.Content=newformurlencodedcontent(param); request.Content.Headers.ContentType
我也与邮递员核实过,api工作正常。
尝试过的解决方案:
(1)
var request=newhttprequestmessage(HttpMethod.Post,url);
request.Content=newformurlencodedcontent(param);
request.Content.Headers.ContentType=新的MediaTypeHeaderValue(“application/x-www-form-urlencoded”);
request.Content.Headers.ContentType.CharSet=“UTF-8”;
使用(HttpResponseMessage response=wait ApiHelper.apiClient.SendAsync(请求))
{
Console.WriteLine(“postRequest->StatusCode->”+response.StatusCode);
if(响应。IsSuccessStatusCode)
{
//ComicModel comic=wait response.Content.ReadAsAsync();
var responseString=await response.Content.ReadAsStringAsync();
返回响应;
}
其他的
{
Console.WriteLine(“postRequest->exception->”+响应);
抛出新异常(response.ReasonPhrase);
}
}
(2)
使用(var content=newformurlencodedcontent(param))
{
content.Headers.Clear();
content.Headers.Add(“内容类型”、“应用程序/x-www-form-urlencoded”);
使用(httpresponsemessageresponse=wait ApiHelper.apiClient.PostAsync(url,内容))
{
Console.WriteLine(“postRequest->StatusCode->”+response.StatusCode);
if(响应。IsSuccessStatusCode)
{
//ComicModel comic=wait response.Content.ReadAsAsync();
var responseString=await response.Content.ReadAsStringAsync();
返回响应;
}
其他的
{
Console.WriteLine(“postRequest->exception->”+响应);
抛出新异常(response.ReasonPhrase);
}
}
}
(3)
使用(HttpResponseMessage response=wait ApiHelper.apiClient.PostAsync(url,新表单UrlEncodedContent(参数)))
{
Console.WriteLine(“postRequest->StatusCode->”+response.StatusCode);
if(响应。IsSuccessStatusCode)
{
//ComicModel comic=wait response.Content.ReadAsAsync();
var responseString=await response.Content.ReadAsStringAsync();
返回响应;
}
其他的
{
Console.WriteLine(“postRequest->exception->”+响应);
抛出新异常(response.ReasonPhrase);
}
}
所有解决方案都会响应400个错误请求。我的参数是:
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")));
var params=new List();
参数添加(新的KeyValuePair(“授权类型”、“密码”);
添加(新的KeyValuePair(“用户名”,HttpUtility.UrlEncode(“myUsername”));
添加(新的KeyValuePair(“密码”,HttpUtility.UrlEncode(“myPass”));
我怎样才能修好它?先谢谢你 你如何通过邮递员认证? 我在你的代码中没有看到它。这取决于api的配置方式。 确保在客户端调用中使用适当的auth方法,例如,Bearer token。Api应该返回400而不是401
httpClient.SetBearerToken(accessToken);
哎呀!我的基本url以http开头。我忘了用https替换它。https的一切都很好。:)
谢谢请与邮递员联系,并检查您正在使用的数据字符串sending@BugFinder是的,我已经和邮递员联系过了。在我的api中一切正常。@BugFinder签出我的邮递员回复我在你的邮递员中添加了邮递员截图,你在3个键的顶部发送了9个标题,其中有什么?@BugFinder只有一个标题是内容类型,值是application/x-www-form-urlencoded,其余都是暂时的请查看我的问题Dmo。我需要api的这个访问令牌。此请求正在调用令牌api以访问此令牌。它可能会帮助您(使用IdentityServer4)
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);
}
}
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")));
httpClient.SetBearerToken(accessToken);