0

When I use the following URI in Postman, I am successfully getting the access_token back, but when I use the same URI in my .NET app, I am getting a 403 message. https://www.reddit.com/api/v1/access_token?grant_type=password&username=xxxx&password=xxxx

Any help will be appreciated. Thanks.

EDIT: Thank you for the link ipodtouch0218. I have added some fields in the header.

Now i am getting a 200 status back but nothing in the Body. Here is the code:

public void Index()
{
 string username = "xxxxx";
 string password = "xxxxx";
 string authString = $"{"xxxxx"}:{"xxxx"}";
 string base64AuthString = Convert.ToBase64String(ASCIIEncoding.UTF8.GetBytes(authString));
 HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, RedditAuth);
 requestMessage.Headers.Clear();
 requestMessage.Content = new StringContent($"?grant_type=password&username={username}&password={password}", Encoding.UTF8, "application/x-www-form-urlencoded");
 requestMessage.Headers.Authorization = new AuthenticationHeaderValue("basic", base64AuthString);
 requestMessage.Headers.Add("User-Agent", "test-app");
 requestMessage.Headers.Add("Accept", "*/*");
 requestMessage.Headers.Add("Accept-Encoding", "*");
 requestMessage.Headers.Add("Connection", "keep-alive");
 HttpClient client = new HttpClient();
 client.DefaultRequestHeaders.Clear();
 client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
 Console.WriteLine(requestMessage);
 var response = client.SendAsync(requestMessage).GetAwaiter().GetResult();
 Console.WriteLine(response);
 return;
}

}

asked Jun 27, 2024 at 18:14
4
  • Does this answer your question? Reddit API returns HTTP 403 Commented Jun 27, 2024 at 18:24
  • Have you tried setting a user agent from a browser? Also, have you tried the same request from a different tool, e.g. Postman or curl? Having a correct API call example usually helps when implementing an integration Commented Jun 30, 2024 at 19:28
  • Yes, i have tried testing with Postman and i do get the body contents back along with the generated access_token. It's only through this console app that the access_token doesn't show up inside the Response.. Commented Jun 30, 2024 at 21:00
  • I found a working solution here: reddit.com/r/redditdev/comments/laonex/… I had to add two things: 1. Install Reddit library from Nuget 2. Added this code in the header section: request.Headers.Add("User-Agent", "test-app"); Thanks. Commented Jul 2, 2024 at 0:43

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.