Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 尝试从GetAsync检索响应时收到HTTP状态代码403_C#_Rest_Api_Oauth - Fatal编程技术网

C# 尝试从GetAsync检索响应时收到HTTP状态代码403

C# 尝试从GetAsync检索响应时收到HTTP状态代码403,c#,rest,api,oauth,C#,Rest,Api,Oauth,我试图使用GetAsync从API获取一些信息,但即使在添加了OAuth授权头和提供给我的令牌之后,我也会得到403的状态代码。我还有别的事要做吗,还是我的代币坏了 class TestAPI { static void Main() { var client = new HttpClient(); client.BaseAddress = new Uri(BASE_ADDRESS_FOR_TESTING); client.Defa

我试图使用
GetAsync
从API获取一些信息,但即使在添加了OAuth授权头和提供给我的令牌之后,我也会得到403的状态代码。我还有别的事要做吗,还是我的代币坏了

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"
        }
    }
}

我觉得你的HttpClient很好。我想你的代币坏了

403错误也支持这一理论

403 禁止服务器理解 请求但拒绝授权

希望公开请求被禁止原因的服务器 可以在响应负载(如果有)中描述该原因。

您还应该打印您的响应消息

        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);
        }
尝试此代码以查看是否在响应内容中获得更多信息


希望这有帮助

我就是这么想的,我只是想核实一下。