Last active
April 8, 2016 11:37
-
-
Save jakobbotsch/70c6de68ba056f31fe7ee7a227711822 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Net; | |
using System.Text; | |
using System.Web; | |
using Newtonsoft.Json.Linq; | |
namespace GiveMeGold | |
{ | |
internal class Program | |
{ | |
private static string ReadPassword() | |
{ | |
// Taken from http://stackoverflow.com/questions/3404421/password-masking-console-application | |
string pass = ""; | |
ConsoleKeyInfo key; | |
do | |
{ | |
key = Console.ReadKey(true); | |
if (key.Key != ConsoleKey.Backspace && key.Key != ConsoleKey.Enter) | |
{ | |
pass += key.KeyChar; | |
Console.Write("*"); | |
} | |
else | |
{ | |
if (key.Key == ConsoleKey.Backspace && pass.Length > 0) | |
{ | |
pass = pass.Substring(0, (pass.Length - 1)); | |
Console.Write("\b \b"); | |
} | |
} | |
} while (key.Key != ConsoleKey.Enter); | |
Console.WriteLine(); | |
return pass; | |
} | |
internal static void Main(string[] args) | |
{ | |
Console.Write("Enter user name: "); | |
string userName = Console.ReadLine(); | |
Console.Write("Enter password: "); | |
string password = ReadPassword(); | |
dynamic login = JObject.Parse( | |
PostToReddit("api/login", null, | |
new Dictionary<string, string> | |
{ | |
["user"] = userName, | |
["passwd"] = password, | |
["api_type"] = "json", | |
["rem"] = "true" | |
})); | |
login = login.json; | |
dynamic errors = login.errors; | |
if (errors.Count > 0) | |
{ | |
Console.WriteLine("Error: {0}", string.Join(", ", errors)); | |
Console.ReadLine(); | |
return; | |
} | |
dynamic data = login.data; | |
string cookie = "reddit_session=" + HttpUtility.UrlEncode((string)data.cookie, Encoding.UTF8); | |
string modhash = data.modhash; | |
const string clientId = "ohXpoqrZYub1kg"; | |
const string redirectUri = "http://localhost:65010/callback"; | |
const string allScopes = | |
"identity,read,vote,report,submit,edit,history,flair,modconfig," + | |
"modflair,modlog,modposts,modwiki,save,mysubreddits," + | |
"privatemessages,subscribe,wikiedit,wikiread,account,creddits"; | |
string guid = Guid.NewGuid().ToString(); | |
PostToReddit("api/v1/authorize", | |
req => | |
{ | |
req.Headers.Add("Cookie", cookie); | |
req.Headers.Add("X-Modhash", modhash); | |
}, | |
new Dictionary<string, string> | |
{ | |
["client_id"] = clientId, | |
["redirect_uri"] = redirectUri, | |
["scope"] = allScopes, | |
["state"] = guid, | |
["duration"] = "permanent", | |
["authorize"] = "allow", | |
["response_type"] = "code", | |
}); | |
Console.WriteLine("Done! Check your Reddit inbox!"); | |
Console.ReadLine(); | |
} | |
private static string PostToReddit(string api, Action<HttpWebRequest> setup, | |
Dictionary<string, string> formParams) | |
{ | |
HttpWebRequest request = WebRequest.CreateHttp($"https://www.reddit.com/{api}"); | |
request.Method = "POST"; | |
request.UserAgent = "RedditAndroid 0.3.2"; | |
request.AllowAutoRedirect = false; | |
setup?.Invoke(request); | |
using (StreamWriter sw = new StreamWriter(request.GetRequestStream())) | |
{ | |
bool first = true; | |
foreach (KeyValuePair<string, string> kvp in formParams) | |
{ | |
if (!first) | |
sw.Write('&'); | |
sw.Write(kvp.Key); | |
sw.Write('='); | |
sw.Write(kvp.Value); | |
first = false; | |
} | |
} | |
using (WebResponse response = request.GetResponse()) | |
{ | |
var respStr = new StreamReader(response.GetResponseStream()).ReadToEnd(); | |
return respStr; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment