Created
July 28, 2013 23:59
-
-
Save dobs/6101369 to your computer and use it in GitHub Desktop.
Simple cookie and modhash generation script.
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
# Simple cookie and modhash generation script. | |
# | |
# Takes a username and password on the command line and generates a | |
# corresponding long-lived password. | |
require 'io/console' | |
require 'multi_json' | |
require 'net/http' | |
printf 'Username: ' | |
user = STDIN.cooked(&:gets).chomp | |
printf 'Password: ' | |
passwd = STDIN.noecho(&:gets).chomp | |
printf "\n\n" | |
http = Net::HTTP.new('www.reddit.com', 80) | |
headers = {'User-Agent' => "Simple Reddit login example, written by /u/dobs"} | |
response = http.post('/api/login', "user=#{user}&passwd=#{passwd}&rem=true&api_type=json", headers) | |
begin | |
data = MultiJson.load(response.body)['json']['data'] | |
puts "Cookie: #{response.get_fields('set-cookie').first}" | |
puts "Modhash: #{data['modhash']}" | |
rescue | |
MultiJson.load(response.body)['json']['errors'].each do |error| | |
puts error.join(': ') | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment