使用Python请求进行Reddit API身份验证

时间:2014-09-10 07:02:59

标签: python-3.x http-authentication reddit

我正在尝试使用我的用户名作为测试对Reddit API进行一些基本的HTTP身份验证,但是当我没有接近极限时,我偶尔会得到429个错误(请求太多)。我部分遵循了本指南:How To use reddit API in python 我不确定我做了什么,但是现在代码末尾的print命令出现语法错误,这与编写指南中的相同。这是我的代码:

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]

抱歉,我知道我可能正在做一些非常愚蠢的事情,感谢您提前帮助我解决问题!哦,这也是Ubuntu 14.04 LTS上的Python-3.4,带有最新版本的Python请求。

1 个答案:

答案 0 :(得分:0)

Reddit API使用OAuth 2.0。尝试使用python oauth2 lib https://pypi.python.org/pypi/python-oauth2/0.7.0

相关问题