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;
}
}
-
Does this answer your question? Reddit API returns HTTP 403ipodtouch0218– ipodtouch02182024年06月27日 18:24:32 +00:00Commented 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 integrationTasos K.– Tasos K.2024年06月30日 19:28:45 +00:00Commented 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..Blofeld– Blofeld2024年06月30日 21:00:14 +00:00Commented 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.Blofeld– Blofeld2024年07月02日 00:43:16 +00:00Commented Jul 2, 2024 at 0:43
lang-cs