尝试从 Reddit API 获取数据时出现 403 错误 [英] 403 error when trying to get data from Reddit API

查看:56
本文介绍了尝试从 Reddit API 获取数据时出现 403 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 oAuth 来验证我的应用程序.我设法获得了一个代码,access_token 和 refresh_token.因此,下一步将尝试获取有关当前用户的信息.

I am using oAuth to authenticate my app. I managed to get a code, access_token and refresh_token. So the next step would be trying to get info about the current user.

    public async void GetCurrentUser()
    {
        using (var client = new HttpClient())
        {
            client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", AccessToken);
            var response = await client.GetAsync("https://oauth.reddit.com/api/v1/me");
            if (response.IsSuccessStatusCode)
            {
                var json = await response.Content.ReadAsStringAsync();
                var obj = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(json);
            }
        }
    }

这是我用来做到这一点的方法.但是,响应始终是 403(禁止)错误代码.知道有什么问题吗?access_token 是我向 https://oauth.reddit.com/api 提出请求时得到的/v1/access_token

This is the method I am using to do that. However the response is always an 403 (Forbidden) error code. Any idea what could be wrong? The access_token is what I got when I made a request to https://oauth.reddit.com/api/v1/access_token

我认为令牌是正确的,因为当我使用 Fiddler 创建相同的请求时,它可以工作.

I think the token is correct because when I create the same request with Fiddler it works.

推荐答案

ANSWER:

通过添加自定义用户代理来修复它

Fixed it by adding a custom user-agent

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, _endpointUri + "me");
            request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", AccessToken);
            request.Headers.Add("User-Agent", Uri.EscapeDataString("android:com.arnvanhoutte.redder:v1.2.3 (by /u/nerdiator)"));

            var response = await client.SendAsync(request);

这篇关于尝试从 Reddit API 获取数据时出现 403 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆