[Answer]-Cookie is not being saved when logging in with the Reddit API

1👍

I’m not exactly sure, but when I’m playing around with the API, I only need to store the modhash and send that in with my requests. Also, if you’re using the requests module, use a session instance instead, and it will store the cookies across calls. I’m not sure how that’d play into Django at all though. Here’s the code I wrote for logging in:

def login(username, password):
    """logs into reddit, saves cookie"""

    print 'begin log in'
    #username and password
    UP = {'user': username, 'passwd': password, 'api_type': 'json',}
    headers = {'user-agent': '/u/STACKOVERFLOW\'s API python bot', }
    #POST with user/pwd
    client = requests.session()

    r = client.post('http://www.reddit.com/api/login', data=UP)

    #print r.text
    #print r.cookies

    #gets and saves the modhash
    j = json.loads(r.text) #I believe r.json == j at this point

    client.modhash = j['json']['data']['modhash']
    print '{USER}\'s modhash is: {mh}'.format(USER=username, mh=client.modhash)

    #pp2(j)

    return client

Leave a comment