I'm trying to log into Reddit and get my own account data.
This my Python code:
from pprint import pprint
import requests
import json
username = 'dirk_b'
password = 'willnottell'
user_pass_dict = {'user': username,
'passwd': password,
'api_type': 'json',
'rem': True, }
headers = {'dirkie': '/u/dirk_b API python test', }
client = requests.session()
client.headers = headers
r = client.post(r'http://www.reddit.com/api/login', data=user_pass_dict)
j = json.loads(r.content.decode());
client.modhash = j['json']['data']['modhash']
s = client.post(r'http://www.reddit.com/api/me.json', data=user_pass_dict)
pprint(s.content)
The response I get is: b'{"error": 404}'
If I do the same request without the .json part. I get a bunch of HTML code with the being 'reddit.com: page not found'. So I assume I'm doing something wrong with the URL. But the URL as I use it is how it is specified in the Reddit API.
The reason I'm not using PRAW is because I eventually want to be able to do this in c++, but I wanted to make sure it works in Python first.
r.content.decode()
? Thejson
library handles the decoding for you.json.loads(r.content)
is just fine; alternatively, usestream=True
toclient.post()
and usejson.load(r.raw)
to have the library read from the http socket directly (may not work if the data is compressed). – Cristionnaclient.modhash
? That won't pass to the server. – Cristionna