Skip to content

Instantly share code, notes, and snippets.

@dobs
Created July 28, 2013 23:59
Show Gist options
  • Save dobs/6101369 to your computer and use it in GitHub Desktop.
Save dobs/6101369 to your computer and use it in GitHub Desktop.
Simple cookie and modhash generation script.
# 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