0

so basically i started learning http request with restsharp and to be honest there is a total confusion.

So i managed to log into my reddit account, catch upvote post request and i tried to post the method and it actually worked on the active session.(yayy after a week...)

Here is my code:

var client = new RestClient("https://www.reddit.com/")           
CookieContainer cookieContainer = new CookieContainer();

            var options = new RestClientOptions()
            {
                MaxTimeout = -1
            };
            var request = new RestRequest("https://oauth.reddit.com/api/vote?redditWebClient=desktop2x&app=desktop2x-client-production&raw_json=1&gilding_detail=1", Method.Post);
            request.AddHeader("accept", "*/*");
            request.AddHeader("accept-language", "en-US,en;q=0.9");
            request.AddHeader("authorization", "Bearer 1909307768910-Y9YshL45Wi1uTiaXGIDzIYS1CvawRw");
            request.AddHeader("content-type", "application/x-www-form-urlencoded");
            request.AddHeader("origin", "https://www.reddit.com");
            request.AddHeader("referer", "https://www.reddit.com/");
            request.AddHeader("sec-ch-ua", "\".Not/A)Brand\";v=\"99\", \"Google Chrome\";v=\"103\", \"Chromium\";v=\"103\"");
            request.AddHeader("sec-ch-ua-mobile", "?0");
            request.AddHeader("sec-ch-ua-platform", "\"Windows\"");
            request.AddHeader("sec-fetch-dest", "empty");
            request.AddHeader("sec-fetch-mode", "cors");
            request.AddHeader("sec-fetch-site", "same-site");
            request.AddHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36");
            request.AddHeader("x-reddit-loid", "0000000000od4gjr66.2.1654778423000.Z0FBQUFBQmlzX05MUTZyNUxqOW4wcnNLNzhiNjFWeG5kR21saVptZXl6RllTcmVuMTJLV2hneEVBU3Z6SDB0VUJOQzJORzE3d0R2aWRHT1YxWFN2akdmbjg4T2Z2RVBUaWR4VjNHRk1paEFaUU9HblQydDBIbXFXZjY3WDAyaS10dGRRaDU0aWFoS2I");
            request.AddHeader("x-reddit-session", "infdgnamhcklpjejeb.0.1656153357171.Z0FBQUFBQml0dVVORWozUVZwQ2RqTGdMc3MwbGJQczJvWW1UVUdmdjVHVjJqMElFV2ZwUlhOaGxmSlh0Wm02aDZsWmx2a0NtVXJKNHEyRG1Hc25tQ0FKMVdlbGlPcDN3aGVXdS1xaXJJRU9KMGVKRFQ1ZXJHTEFzSXpqSm1RUmFIa0dCNFBjU2tnYlo");
            request.AddHeader("Cookie", "csv=2; edgebucket=EzrHckI9fWuza20bDr; loid=0000000000p5fofqq6.2.1656153609022.Z0FBQUFBQml0dVlKM2FDVUo5NUN2eEZBRDk4NDV5WWtDNk1mU01vRmktRnB1eWYwMzY4X3ZCdkx1eXZUNERzTVN1MkhDZXdnZFpHblMwU0k4LTc4M1VnNGRkQ3hOQUp3dTVxaFZYbndFQ19WN3NUaUFsX21MaE1SNTRaQWRsQU1sZFBUTDJQWlBoRnM; session_tracker=zoCNAuHADuPm2Hy4AF.0.1656153616293.Z0FBQUFBQml0dVlRRTZCc2NBNHRmV2xxUWhwVFllT0JaUGhDN3JDNTYxRk1DLURsTmJ4U1ZVOUNLRjN4RjZHNExpZUZjcVd6MzktMGVZMS12X193SDJ2OUd0R19kVjJ3dEtnWGFPOEVHSWd5dHBIWFVZcWNReEZPb0Q5UG9mRVlyWUFXOC1lMEYxZ20");
            request.AddParameter("id", "t3_qbl6b2");
            request.AddParameter("id", "t3_vk7gmb");
            request.AddParameter("dir", "1");
            request.AddParameter("api_type", "json");
            RestResponse response = client.Execute(request);

            Console.WriteLine(response.Content);

Ok the thing here is im not really interested into official reddit api or any official api as im only trying to learn and i have no precise goal. I have some questions that i need to be precised. I found some explanations on youtube, here or google but its not really clear and i seen that restsharp have updated recently.

1- So far what i understand, its that i can do this post request because the active session is using the same Bearer and cookies but i guess intercepting the post request each time to generate these data aint really practical so how you actually get the session id/loid/cookies/bearer and all dynamic data that you need to make succesful request before actually doing the request(logic lol)?

2- How to handle cookies?

3- Do you need to make a get request before a post?

4- How to handle objects and data from the response?

I want to learn so if you guys have some knowledge to share, it will be appreciated!

  • Search for "OAuth" (specifically OAuth 2). Reddit access tokens last for 1 hour from what I can understand. – ProgrammingLlama Jun 26 '22 at 09:46
  • I have read all the documents: Oauth, Oauth2 and so on... it just dont wanna work.... pretty frustrating but thx for your answer. – HomerSimpsons Jun 27 '22 at 08:32
  • Maybe I misunderstood your post, but it sounded like you were thinking you needed to obtain a new access token for every request, which certainly isn't the case. – ProgrammingLlama Jun 27 '22 at 08:49
  • No let me reformulate i know it was a long post. Let say you want to automate a website, you make a code to do so. To make succesfull request, you need bearer token and cookies as far i understand. How you manage to update them to be valid. I mean if i go to google and inspect network and intercept post, i will find the actual cookies and valid bearer tokens but how you request these valid data from code? They only last 1 hour so sometime you need to update them. Dont know if its more clear sry if its not and thx again for your answer! – HomerSimpsons Jun 27 '22 at 08:56
  • When you login with oauth you usually get an access token (short lived) and a refresh token (long lived). When the access token expires (when you get a 401 status from the server) you can use that as a trigger to use the refresh token to obtain a new access token. Some services replace the refresh token at the same time, some reuse it. Generally if you're using OAuth, you won't be using cookies for authentication. – ProgrammingLlama Jun 27 '22 at 09:03
  • i tried to login with this: RestClient client = new RestClient("https://www.reddit.com"); client.Authenticator = new HttpBasicAuthenticator("username", "pass"); RestRequest request = new RestRequest("https://www.reddit.com/login", Method.Get); request.AddHeader("content-type", "application/x-www-form-urlencoded"); RestResponse restResponse = client.Execute(request); ResponseStatus responseStatus = restResponse.ResponseStatus; Console.WriteLine(responseStatus.ToString()); Dont work – HomerSimpsons Jun 27 '22 at 09:05
  • code is too long but i tried post method too and it give bad request. I dont know what to do sincerely – HomerSimpsons Jun 27 '22 at 09:08
  • it seem that reddit dont accept basic authenticator do you have an idea??? – HomerSimpsons Jun 27 '22 at 09:09
  • If Reddit is indeed using OAuth then I wouldn't expect them to also support Basic authentication. – ProgrammingLlama Jun 27 '22 at 09:12
  • So in this case what we do? Sry for all these questions, im learning! – HomerSimpsons Jun 27 '22 at 09:16
  • Login with OAuth. – ProgrammingLlama Jun 27 '22 at 09:18
  • var client = new RestClient("http://example.com"); client.Authenticator = OAuth1Authenticator.ForRequestToken(consumerKey, consumerSecret); this is oAuth1 login but as i said, how i get the consumerKey and consumer secret before login there is my problem – HomerSimpsons Jun 27 '22 at 09:30

1 Answers1

0

Ok so it was totally not about oauth at all. -So basically, you need to make a GET request that will deliver an html code.

-In this html code you will find an csrf token. You will use regex to put it in a string.

-You will create a POST login request that return username, password and csrf token.

You're now logged!!!

About handling cookies, restsharp do it automatically as long as you keep the same RestClient object. I put them in a string array and use them in the request using String.Format