python - Reddit API 和投票。不接受 modhash/cookie。 .error.USER_REQUIRED

标签 python api reddit

我试图让投票 API 正常工作,但收到错误 .error.USER_REQUIRED。不明白为什么,但我认为我必须以错误的方式发送 modhash 或 session cookie,因为登录顺利

我的代码看起来像这样:

UP = {'user': username, 'passwd': password, 'api_type': 'json',}

client = requests.session()

r = client.post('http://www.reddit.com/api/login', data=UP)

j = json.loads(r.text)

mymodhash = j['json']['data']['modhash']

url = 'http://www.reddit.com/api/vote/.json'
postdata = {'id': thing, 'dir': newdir, 'uh': mymodhash}
vote = client.post(url, data=json.dumps(newdata))

错误:

{"jquery": [[0, 1, "refresh", []], [0, 2, "attr", "find"], [2, 3, "call", [".error.USER_REQUIRED"]], [3, 4, "attr", "show"], [4, 5, "call", []], [5, 6, "attr", "text"], [6, 7, "call", ["please login to do that"]], [7, 8, "attr", "end"], [8, 9, "call", []]]}

最佳答案

要登录,您应该发布到ssl.reddit.com,这样您就不会以纯文本形式发布您的凭据。另外,您应该设置一个用户代理。

以下是对您的/r/redditdev 提交进行投票的工作示例。

import requests
# Login                                                                                                
client = requests.session(headers={'User-Agent': 'Requests test'})
data = {'user': 'USERNAME', 'passwd': 'PASSWORD', 'api_type': 'json'}
r = client.post('https://ssl.reddit.com/api/login', data=data)
modhash = r.json['json']['data']['modhash']

# Vote                                                                                                 
data = {'id': 't3_11mr32', 'dir': '1', 'uh': modhash, 'api_type': 'json'}
r = client.post('http://www.reddit.com/api/vote', data=data)
print r.status_code  # Should be 200                                                                   
print r.json  # Should be {}

此外,除非您真的对 reddit 的 API 幕后工作原理感兴趣,否则我建议您使用 PRAW .

关于python - Reddit API 和投票。不接受 modhash/cookie。 .error.USER_REQUIRED,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12936274/

相关文章:

java - 谷歌云端硬盘 API : How can I have an access token without asking for Authorization everytime

c++ - EverNote API 编译错误

node.js - 需要帮助使用 nodejs 设置 reddit oauth

python - 如何获得Python算法的数学方程式?

python - Pandas:使用 groupby 或数据透视表时按顺序排列日期

python - 通过 Nibabel 加载 Nifti 并使用形状函数

node.js - 通过浏览器和 NodeJS 请求访问 Reddit json api 时,其行为有所不同

python - 变量的显示值

php - 如何在 PHP 中实现这个歌词 API?

javascript - 为什么在 Node.js 上使用 superagent 尝试登录 reddit 失败?