C# 获取错误:不支持使用httpclient在asp.net控制台应用程序中发布formdata的\u授予\u类型
我找遍了所以都没有用,还是没有办法。 我正在尝试使用asp.net控制台应用程序和httpclient从第三方服务获取身份验证令牌。 在Postman中一切都正常,但在c中复制相同的内容时返回C# 获取错误:不支持使用httpclient在asp.net控制台应用程序中发布formdata的\u授予\u类型,c#,asp.net,httpclient,C#,Asp.net,Httpclient,我找遍了所以都没有用,还是没有办法。 我正在尝试使用asp.net控制台应用程序和httpclient从第三方服务获取身份验证令牌。 在Postman中一切都正常,但在c中复制相同的内容时返回“{\'error\:\“unsupported\u grant\u type\”} 这是我的密码 using (var httpClient = new HttpClient()) { httpClient.DefaultRequestHeader
“{\'error\:\“unsupported\u grant\u type\”}
这是我的密码
using (var httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
var multiform = new MultipartFormDataContent()
{
{new StringContent("password"), "grant_type" },
{new StringContent("adfadsasdfas"), "username" },
{new StringContent("asdfasdadf"), "password" },
};
var response = Task.Run(() => httpClient.PostAsync("http://localhost:61216/token", multiform)).Result;
var message = Task.Run(() => response.Content.ReadAsStringAsync()).Result;
if (!response.IsSuccessStatusCode)
{
throw new HttpRequestException("Unable to post to cliams manager");
}
}
期待中的感谢。。。
抱歉,我已将授权类型更新为密码。它实际上不是grant_类型,因为“password”也不起作用
问题似乎在于这部分代码:
var multiform = new MultipartFormDataContent()
{
{new StringContent("password"), "grant_type" },
{new StringContent("adfadsasdfas"), "username" },
{new StringContent("asdfasdadf"), "password" },
};
您应该改为使用此选项:
var content = new FormUrlEncodedContent(
new KeyValuePair<string, string>[] {
new KeyValuePair<string, string>("grant_type", "password"),
new KeyValuePair<string, string>("username", "asdfsad"),
new KeyValuePair<string, string>("password", "asdfaasdfa")
}
);
var content=new FormUrlEncodedContent(
新的KeyValuePair[]{
新的KeyValuePair(“授权类型”、“密码”),
新的KeyValuePair(“用户名”、“asdfsad”),
新的KeyValuePair(“密码”、“asdfaasdfa”)
}
);
在OAuth 2中,我们将授权类型设置为“密码”当我们发送用户id和密码以检索访问令牌,并且您以不正确的格式发送数据时,您已将内容类型指定为application/x-www-form-urlencoded
,但您正在POST
中发送多部分/表单数据
您可以在此处阅读有关密码授予类型的更多信息
因为您设置的授权类型不正确,很难知道原因。但是,如果这可以帮助您,那么有一种方法可以使用Postman生成代码。如果您单击“保存”右侧的“代码”按钮(相同的授权类型适用于邮递员,授权类型不是真实值)编辑问题后,授权类型=“密码”在第一次尝试时也不起作用