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": "Cool post, but here's an updated to the post's body",
      "edit_reason": "I changed this because I can."
   }
}
"""

resp = requests.put(url, headers=headers, data=data)

print(resp.status_code)

["BAD CSRF"] 오류가 발생합니다.

저는 관리자이며 제 키는 global입니다. 가능하면 권한이 덜 있는 키로 이 작업을 수행하고 싶습니다.
이 게시물은 API를 통해 생성한 토픽의 첫 번째이자 유일한 게시물입니다.

미리 감사드립니다 :slight_smile:

왜 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":["You are not permitted to view the requested resource. The API username or key is invalid."],"error_type":"invalid_access"}

답변 주셔서 감사합니다!