[英]I am getting StatusCode: 401 “Unauthorized” in a HttpClient.PostAsync (C#)
I am trying to use an Api Rest (IGDB) making an Http Post Request with HttpClient;我正在尝试使用 Api Rest (IGDB) 使用 HttpClient 发出 Http 发布请求; this request needs to have a key and a body.
这个请求需要有一个key和一个body。 I am providing the key in HttpClient.DefaultRequestHeaders.Authorization, but I am getting a 401 status code, I know that the key works because I have used it in Postman and It worked fine, so I must be implementing it wrong.
我在 HttpClient.DefaultRequestHeaders.Authorization 中提供了密钥,但我得到了 401 状态代码,我知道该密钥有效,因为我已经在 Postman 中使用它并且它工作正常,所以我必须错误地实现它。
My code:我的代码:
private async Task<string> ConsumeApi()
{
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Key Name", "Key Value");
//Makes the client request only Json data
//client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("aplication/json"));
string theUri = "https://api-v3.igdb.com/games";
var stringcontent = new StringContent("fields name", UnicodeEncoding.UTF8,"application/json");
var response = await client.PostAsync("https://api-v3.igdb.com/games", stringcontent);
return response.ToString();
}
And these are Postman pictures of what I am trying to implement (works fine):这些是我正在尝试实现的 Postman 图片(工作正常):
AuthenticationHeaderValue
AuthenticationHeaderValue
is not setting a header but is an authorization header.没有设置 header 而是授权 header。 Set a normal header value, not one prefixed with Authentication.
设置一个普通的 header 值,而不是一个以 Authentication 为前缀的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.