我在 HttpClient.PostAsync (C#) 中收到 StatusCode: 401 "Unauthorized"

I am getting StatusCode: 401 "Unauthorized" in a HttpClient.PostAsync (C#)

我正在尝试使用 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();
        }

这些是我正在尝试实现的 Postman 图片(效果很好):

AuthenticationHeaderValue

不是设置header而是授权header。设置一个正常的 header 值,而不是带有 Authentication 前缀的值。