Python+HTTPRequests+Reddit

  • Sun 18 September 2011

  • This is a piece of a puzzle if you will. I needed a way to get to Reddit's JSON API with Python. I decided to use HTTPRequests for it's simplicity. Reddit's API told me what I needed to pass. I'll admit here now that my experience in Python/JSON/API/Whatever is VERY weak, I guess I'd like more documentation/examples, etc. etc. but I guess I can't have my cake and eat it too!

    As I said this is the first piece of a puzzle. Simply a means to document this now, I'm working on a github repository which right now is purely tests. But if you wanna follow me now, I'm certain I'll get things sorted out, and this code posted up and it'll obviously grow as the project progresses. No I won't tell you what the project is yet. In time, you'll likely figure it out.

    import requests
    import json
    uname = 'UserABC123'
    upasswd = 'Itsasecret!'
    login\_url = 'http://www.reddit.com/api/login/+uname'
    self\_info\_url = 'http://www.reddit.com/api/me.json'
    params = {
    'api\_type': 'json',
    'passwd': upasswd,
    'user': uname
    }
    rlogin=requests.post(login\_url, params)
    

    Once I'm connected I can call 'rlogin' and it'll return 'modhash' and 'cookie'. With this, I can put the cookie in a "cookie jar" and do what I need to. Which I will post here shortly once I get the code cleaned up.

        >>>
        >>> rlogin
        <Response [200]>
        >>> rlogin.content
        '{"json": {"errors": [], "data": {"modhash":
    "ofofofoofofofoffooffofofofofofofofoffofofofof", "cookie":
    "12345678,2011-09-18T18:33:54,9ofofofofofofofofofoffofofofofofofofoffo"}}}'
    

    Comments !

    social