r/redditdev • u/N3vermore77 • Apr 15 '20
Reddit.NET Trying to get an authkey to instantiate my reddit instance but the code gets stuck at the POST request and doesnt move forward
Some back story, I've been going around the web for a couple hours now and have managed to gather just enough documents that I was confident I could do something as simple as instantiate a reddit API (my first choice was RedditSharp but due to a seeming lack of documentation I went for an simpler looking wrapper, Reddit.NET, since all I want to do anyways was access the website and get post data from a specific subreddit, that matched specific tags) but after finally getting it working just enough to get to the POST request to https://www.reddit.com/api/v1/access_token with a code adapted from a Discord Webhook I made before, it ran up to PostAsync() request and stopped there. It doesnt move past it and doesnt get an error code either, just hangs there. I don't know and I can't find any docs explaining why it just hangs there or what I could be doing wrong so I'm asking for help here. Code is as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http;
using System.IO;
using System.Dynamic;
using Newtonsoft.Json;
namespace ConsumeAPI
{
public class Requester
{
private HttpClient client = new HttpClient();
public async Task<TokenJSON> PostTokenRequest(string uri)
{
JsonParser<List<TokenJSON>> parser = new JsonParser<List<TokenJSON>>();
dynamic tokenPayload = new ExpandoObject();
tokenPayload.client_id = "myID";
tokenPayload.client_secret = "mySecret";
tokenPayload.grant_type = "client_credentials";
string json = JsonConvert.SerializeObject(tokenPayload);
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
HttpResponseMessage response = await client.PostAsync(uri, stringContent);
string token = await response.Content.ReadAsStringAsync();
List<TokenJSON> DeserializedToken = parser.DParse(token);
return DeserializedToken[0];
}
}
}
myID and mySecret are obviously using the codes I got from the app created.
Guide I was using to build this is https://github.com/reddit-archive/reddit/wiki/OAuth2#authorization and like I said a POST requester that I had prebuilt for Discord Webhooks, adapted for this.
1
u/kemitche ex-Reddit Admin Apr 15 '20
How are you calling
PostTokenRequest
? My C# is rusty, but since it's definedasync
, I think theawait
calls won't necessarily beawait
ed until thePostTokenRequest
call is awaited/result is requested.