Getting error: unsupported_grant_type using httpclient to post formdata in asp.net console app
Getting error: unsupported_grant_type using httpclient to post formdata in asp.net console app
我找遍了SO都没有用,还是没有解决办法。
我正在尝试使用 asp.net 控制台应用程序和 httpclient 从第三方服务获取身份验证令牌。
在 Postman 中一切正常,但在 c# 中复制相同 returns "{\"error\":\"unsupported_grant_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_type 更新为密码。它实际上不是 grant_type 因为 "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")
}
);
在 OAuth 2 中,当我们发送用户 ID 和密码以检索访问令牌时,我们将 grant_type
设置为“密码”,而且您发送的数据格式不正确您已将内容类型指定为是 application/x-www-form-urlencoded
但你在 POST
.
中发送 multipart/form-data
您可以在此处阅读有关密码授予类型的更多信息
https://www.rfc-editor.org/rfc/rfc6749#section-4.3.2
我找遍了SO都没有用,还是没有解决办法。
我正在尝试使用 asp.net 控制台应用程序和 httpclient 从第三方服务获取身份验证令牌。
在 Postman 中一切正常,但在 c# 中复制相同 returns "{\"error\":\"unsupported_grant_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_type 更新为密码。它实际上不是 grant_type 因为 "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")
}
);
在 OAuth 2 中,当我们发送用户 ID 和密码以检索访问令牌时,我们将 grant_type
设置为“密码”,而且您发送的数据格式不正确您已将内容类型指定为是 application/x-www-form-urlencoded
但你在 POST
.
multipart/form-data
您可以在此处阅读有关密码授予类型的更多信息 https://www.rfc-editor.org/rfc/rfc6749#section-4.3.2