如果有人能指出我正确的方向,我将非常感激。我正在尝试使用请求模块与Reddit的API进行交互,但出于某种原因,我获得了HTTP状态403
代码。我能够成功登录我相信(因为我得到HTTP 200),但我无法执行成功的API请求。我是新手,所以请用小词。感谢。
import requests
import json
from pprint import pprint
# URLs
url_base = r'https://www.reddit.com'
url_login = '/api/login'
url_me = '/api/v1/me'
# Credentials
user = '__kitten_mittens__'
passwd = 'password'
params = {'user': user,
'passwd': passwd,
'api_type': 'json',}
headers = {'user-agent': '/u/__kitten_mittens__ practice api bot'}
# Set up the session/headers
client = requests.session()
client.headers = headers
response = client.post(url_base + url_login, data = params)
j = json.loads(response.content.decode('utf-8'))
mh = j['json']['data']['modhash']
print("The modhash for {USER} is {mh}".format(USER=user, mh=mh))
# THIS CODE GIVES HTTP STATUS CODE 403
response = client.get(url_base + url_me)
me_json = json.loads(r.content.decode('utf-8'))
pprint(me_json)
编辑:我修复了密码字段中缺少的单引号。这不是问题的一部分。