Arn Vanhoutte
Arn Vanhoutte

Reputation: 1927

403 error when trying to get data from Reddit API

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);
            }
        }
    }

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

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

Upvotes: 0

Views: 1146

Answers (1)

Arn Vanhoutte
Arn Vanhoutte

Reputation: 1927

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);

Upvotes: 0

Related Questions