0

I'm trying to do some basic HTTP authentication with the Reddit API using my username as a test, but I still get 429 errors (too many requests) occasionally when I'm nowhere near the limit. I was partially following this guide: How To use reddit API in python I'm not sure what I did, but I'm getting a syntax error with the print command at the end of the code now, which is written the same as in the guide. Here's my code:

CLIENT_ID = "<CLIENT_ID>"
CLIENT_SECRET = "<CLIENT_SECRET>"
REDIRECT_URL = "http://localhost:65010/reddit_callback"

import pprint
import json
import requests
import requests.auth

headers = {'user-agent': 'Test Script by /u/<USERNAME>'}

client_auth = requests.auth.HTTPBasicAuth(CLIENT_ID, CLIENT_SECRET)

post_data = {"grant_type": "password", 
             "username": "<USERNAME>", "password": "<PASSWORD>"}

response = requests.post("https://ssl.reddit.com/api/v1/access_token", 
                         auth=client_auth, data=post_data)

client = requests.session()
client.headers = headers

r = client.get(r'http://www.reddit.com/user/<USERNAME>/about/.json')
r.text
data = r.json()

print data['data']['children'][0]

Sorry, I know I'm probably doing really stupid things, thanks in advance for your help in sorting me out! Oh, also, this is Python-3.4 on Ubuntu 14.04 LTS with the latest version of Requests for Python.

flying sheep
  • 8,475
  • 5
  • 56
  • 73
  • "I'm getting a syntax error with the print command at the end of the code now" - Can you include the actual error in your post? If I had to guess, you're maybe using Python 3 and the example code is written for Python 2. – kemitche Sep 11 '14 at 19:00

1 Answers1

0

Reddit API use OAuth 2.0. Try with python oauth2 lib https://pypi.python.org/pypi/python-oauth2/0.7.0.

scalloty
  • 108
  • 5