我正在尝试使用Api Rest(IGDB)通过HttpClient发出Http Post请求;该请求需要有一个钥匙和一个主体。我在HttpClient.DefaultRequestHeaders.Authorization中提供了密钥,但是我得到的是401状态代码,我知道该密钥有效,因为我在Postman中使用了它,并且它工作正常,因此我必须错误地实现它。
我的代码:
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();
}
您不会想到:
AuthenticationHeaderValue
不是设置标头而是授权标头?设置一个普通的头值,而不是一个以Authentication为前缀的值。