我正在尝试创建一个脚本来获取特定主题的帖子。请参阅此页面 Discourse API Docs
但我一直收到响应状态码 = 413。
有人能帮帮我吗!如果你能解决,我会给你 PayPal 账户发送 20 美元。
def get_post_from_topic(topic_id, post_id):
endpoint = "https://www.example.com/t/" + topic_id + "/posts.json"
headers = {
"Content-Type": "application/json",
"Api-Key": api_key,
"Api-Username": api_username,
}
data = {"post_ids[]": post_id} # 我试过 0,没用
response = requests.get(endpoint, headers=headers, data=data)
if response.status_code == 200:
return response
供您参考,我的 Api-Key 和 Api-Username 是正确的。我的其他 discourse API 都正常工作,唯独这个不行。
您好,您可以查看 Discourse API gen
您可以在此 gem 中轻松找到所需的 API,因此无需单独实现。
如果您仍需要付费支持,请告知我们,我们会将主题移至 Marketplace,以便感兴趣的人士与您联系。
1 个赞
pfaffman
(Jay Pfaffman)
7
只需获取主题,然后通过忽略其他帖子来整理您想要的帖子。
另外,如果您有帖子 ID,则可以获取 /p/123
我尝试了检索单个帖子的 API。它不返回“link_counts”并且没有“post_stream”。
我认为从主题 API 获取特定帖子具有“post_stream”。
pfaffman
(Jay Pfaffman)
10
您可能需要显式地将数据编码为 json?
data = '{\"post_ids[]\":' + post_id +'}'
RGJ
(Richard - Communiteq)
13
GET 始终带有查询字符串,从不包含 POST 数据(这就是您收到 413 Payload Too Large 错误的原因,它不期望任何数据)。在查询服务器数据而不进行更改时,您几乎总是使用 GET 而不是 POST。因此,您应该使用 params= 而不是 data=。
我认为此特定 API 调用的文档措辞不当(“请求正文架构”实际上表明是 POST data),因此我将其移至 Bug,您无需向我支付任何费用。
import requests
import json
def get_post_from_topic(topic_id, post_id):
endpoint = f"https://forum.example.com/t/{topic_id}/posts.json"
api_username = 'system'
api_key = 'REDACTED'
headers = {
"Content-Type": "application/json",
"Api-Key": api_key,
"Api-Username": api_username,
}
params = {
"post_ids[]": post_id
}
response = requests.get(endpoint, headers=headers, params = params)
if response.status_code == 200:
return response
response = get_post_from_topic(6,8)
print(response.json())
{"post_stream": {"posts": [{"id": 8, "name": "system", "username": "system", "avatar_template": "/images/discourse-logo-sketch-small.png", "created_at": "2022-06-26T04:44:23.637Z", "cooked": "<p><a name=\"collect\"></a></p>\n<h2>\n<a name=\"what-information-do-we-collectcollect-1\" class=\"anchor\" href=\"#what-information-do-we-collectcollect-1\"></a><a href=\"#collect\">What information do we collect...\n```
5 个赞
system
(system)
关闭
17
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.