gpt4 book ai didi

c# - 获取错误 : unsupported_grant_type using httpclient to post formdata in asp. 网络控制台应用程序

转载 作者:行者123 更新时间:2023-12-04 23:24:24 25 4
gpt4 key购买 nike

我找遍了SO都无济于事,仍然没有解决方案。我正在尝试使用 asp.net 控制台应用程序和 httpclient 从第三方服务获取身份验证 token 。在 Postman 中一切正常,但在 C# 中复制相同内容会返回 "{\"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,因为“密码”也不起作用。

enter image description here

最佳答案

问题似乎出在这部分代码上:

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")
}
);

关于c# - 获取错误 : unsupported_grant_type using httpclient to post formdata in asp. 网络控制台应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59390594/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com