get paid to paste

Part 1, logging in to the reddit API

#importing all the modules
from pprint import pprint
import requests
import json
 
#set username and password values
username = 'YOURUSERNAME'
password = 'YOURPASSWORD'
 
#create dict with username and password
user_pass_dict = {'user': username,
                  'passwd': password,
                  'api_type': 'json',}
 
#set the header for all the following requests
headers = {'user-agent': '/u/TankorSmash\'s API python tutorial bot', }
 
#create a requests.session that'll handle our cookies for us
client = requests.session()
 
#make a login request, passing in the user and pass as data
r = client.post(r'http://www.reddit.com/api/login', data=user_pass_dict)
 
#optional print to confirm error-free response
#pprint(r.text)
 
#turns the response's JSON to a native python dict
j = json.loads(r.text)
 
#grabs the modhash from the response
client.modhash = j['json']['data']['modhash']
 
#prints the users modhash
print '{USER}\'s modhash is: {mh}'.format(USER=username, mh=client.modhash)

Pasted: Aug 28, 2012, 5:10:54 am
Views: 327