[英]HTTP status code 403 received when trying to retrieve response from GetAsync
I am trying to get some information from an API using GetAsync
, but I am getting a status code of 403 even after adding the OAuth authorization header with the token provided for me. 我正在尝试使用GetAsync
从API获取一些信息,但是即使在添加带有为我提供的令牌的OAuth授权标头后,我仍得到403的状态代码。 Is there something else I need to do, or is my token bad? 还有什么我需要做的,还是令牌不好?
class TestAPI
{
static void Main()
{
var client = new HttpClient();
client.BaseAddress = new Uri(BASE_ADDRESS_FOR_TESTING);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("OAuth", TOKEN);
HttpResponseMessage response = await client.GetAsync("WebApi/CaseLogs/v10/Search?DateFrom=04-05-2012");
if (response.IsSuccessStatusCode)
{
result = await response.Content.ReadAsStringAsync();
}
else
{
Console.WriteLine((int) response.StatusCode); // prints "403"
}
}
}
Your HttpClient looks fine to me. 您的HttpClient在我看来还不错。 I think your Token is bad. 我认为您的令牌不好。
The 403 Error also supports this theory. 403错误也支持该理论。
403 FORBIDDEN The server understood the request but refuses to authorize it. 403 FORBIDDEN服务器理解了请求,但拒绝授权。
A server that wishes to make public why the request has been forbidden can describe that reason in the response payload (if any). 希望公开为什么禁止请求的服务器可以在响应有效负载(如果有)中描述该原因。 https://httpstatuses.com/403 https://httpstatuses.com/403
You should also print your response message. 您还应该打印响应消息。
using ( HttpResponseMessage response = await client.GetAsync("WebApi/CaseLogs/v10/Search?DateFrom=04-05-2012"))
using (HttpContent content = response.Content)
{
string result = await content.ReadAsStringAsync();
Console.WriteLine(result);
}
Try this code to see if you get more information in the response content. 尝试使用此代码,以查看您是否在响应内容中获得了更多信息。
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.