大家好,我在更新话题时遇到了问题。我能够获取到第一个帖子的 id,如此处所示。通过运行以下代码可以确认这一点:
import requests
from requests.structures import CaseInsensitiveDict
url = "https://my/discourse/instance/posts/{post_id}.json"
headers = CaseInsensitiveDict()
headers["Authorization"] = "{\"api-key\": \"{le_api_key}\", \"api-username\": \"{le_username}\"}"
resp = requests.get(url, headers=headers)
print(resp.status_code)
该代码返回了 200 状态码以及我预期的关于帖子的信息。
但当我尝试以下操作时:
import requests
from requests.structures import CaseInsensitiveDict
url = "https://my/discourse/instance/posts/{post_id}.json"
headers = CaseInsensitiveDict()
headers["Authorization"] = "{\"api-key\": \"le_api_key\", \"api-username\": \"le_username\"}"
headers["Content-Type"] = "application/json"
data = """
{
"post": {
"raw": "很棒的帖子,但这里更新了帖子的正文",
"edit_reason": "我更改了它,因为我可以。"
}
}
"""
resp = requests.put(url, headers=headers, data=data)
print(resp.status_code)
我收到了 ["BAD CSRF"] 错误。
我是管理员,我的密钥是 global。理想情况下,我希望使用权限较低的密钥来运行此操作。
该帖子是我通过 API 创建的话题中的第一个也是唯一一个帖子。
非常感谢大家 :-)"