简体   繁体   English

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

[英]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 图片(工作正常): 图3

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.

相关问题 没有得到HttpClient.PostAsync()的结果 - Not getting result of HttpClient.PostAsync() C#PostAsync - {StatusCode:401,ReasonPhrase:与ASP.NET MVC 4&#39;未授权&#39; - C# PostAsync - {StatusCode: 401, ReasonPhrase: 'Unauthorized' with ASP.NET MVC 4 httpClient.PostAsync(URL,content)在C#Metro应用中不起作用 - httpClient.PostAsync(url, content) not working in C# Metro apps HttpClient.PostAsync() 方法在 C# xUnt 测试中永远挂起 - HttpClient.PostAsync() method is hanging forever in C# xUnt test C# HttpClient.PostAsync 不返回或抛出错误 - C# HttpClient.PostAsync doesn't return or throw an error HttpClient.PostAsync Xamarin.Android C#错误响应 - HttpClient.PostAsync Xamarin.Android C# wrong response C#HttpClient.PostAsync只工作一次 - c# HttpClient.PostAsync only works once StatusCode:401,ReasonPhrase:使用C#通过HTTPClient调用Post方法时,显示“未授权” - StatusCode: 401, ReasonPhrase: 'Unauthorized' gets displayed when calling a Post Method through HTTPClient using C# PostAsync(承载身份验证)后未经授权的HttpClient 401 - HttpClient 401 Unauthorized after PostAsync (Bearer Auth) 为什么我的 C# winforms 应用程序中的第一个 HttpClient.PostAsync 调用非常慢? - Why is first HttpClient.PostAsync call extremely slow in my C# winforms app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM