且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 Reddit API 中请求 json 时出现 404 错误

更新时间:2021-11-21 10:05:48

/api/me.json route 只接受 GET 请求:

The /api/me.json route only accepts GET requests:

s = client.get('http://www.reddit.com/api/me.json')

该端点没有 POST 路由,因此您将收到 404.

There is no POST route for that endpoint, so you'll get a 404 for that.

另外,如果需要将modhash传递给服务器,在POST请求中传递的数据中进行;设置client.modhash然后将该参数传递给服务器.您从 me.json GET 响应中检索 modhash:

Also, if you need to pass modhash to the server, do so in the data passed in the POST request; setting client.modhash does not then pass that parameter to the server. You retrieve the modhash from your me.json GET response:

r = client.get('http://www.reddit.com/api/me.json')
modhash = r.json()['modhash']

注意来自 requests 的响应如何具有 .json() 方法,无需自己使用 json 模块.

Note how the response from requests has a .json() method, there is no need to use the json module yourself.

然后在 POST 请求数据中使用 modhash:

You then use the modhash in POST request data:

client.post('http://www.reddit.com/api/updateapp', {'modhash': modhash, 'about_url': '...', ...})