ControlC ControlC.com

Part 1, logging in to the reddit API

Views: 685Total Chars: 998
Created: 8/28/12 1:10:54AM ET
Self Destructs: Never
#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)
998 chars, 120 words, 34 lines·Create new version