使用API更新主题时收到["BAD CSRF"] [python]

大家好,我在更新话题时遇到了问题。我能够获取到第一个帖子的 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 创建的话题中的第一个也是唯一一个帖子。

非常感谢大家 :-)"

你为什么把 headers 嵌套在这个 Authorization 字典键下?Discourse REST API Documentation 的文档中并未说明这一点::thinking:

你说得对,这确实是我用来测试 API 的工具正在做的事情,而且它能正常工作,这本身似乎就是一个问题:

header1 = CaseInsensitiveDict()
header1["Authorization"] = '{"api-key": "longapikey", "api-username": "myusername"}'

header2 = {"api-key": "longapikey", "api-username": "myusername"}

r = requests.get(url, headers= HEADER)

HEADER 等于 header1 时,它能正常工作;而当它等于 header2 时,我会收到以下错误:

{"errors":["您无权查看请求的资源。API 用户名或密钥无效。"],"error_type":"invalid_access"}

顺便说一句,谢谢你的回复!