Discourse API를 사용하여 주제 생성

안녕하세요
도와주세요.
Discourse API를 사용하여 주제를 생성하려고 하지만, 시도할 때마다 실패합니다. 제 코드는 다음과 같습니다:

https://myDiscourse.com/posts.json?title=New+test+posted&raw=new+posted+text+to+test+creation+by+api&category=45&api_key=xxxxxxxxxxxxxxxxxxxxxxxx&api_username=admin

무엇이 문제인지 모르겠습니다. 도와주실 수 있을까요?

감사합니다

URL에 API 키를 포함할 수 없습니다(웹 트래픽을 볼 수 있는 모든 사람에게 키가 노출됩니다). 키는 헤더를 통해 전달해야 합니다.

예를 확인하려면 Grant a custom badge through the API 을 참조하세요.

안녕하세요 @pfaffman
도움 주셔서 감사합니다. 예시에 따라 시도해 보았지만 오류가 발생합니다:

401 Authorization Required

제 스크립트:

$apikey = "xxxxxxxxxxxxxxxxxxxx";
	$username = "xxxxxxxxxxxxxxxxx";
	
	$title = "test";
	$raw = "message";
	$category = 9;
	
	$url = 'https://myDiscourse.com/posts.json';
	$post_fields = array(
		'title' => $title,
		'raw' => $raw,
		'category' => $category,
	);
	$headers = array("Content-Type: multipart/form-data;","Api-Key: $apikey","Api-Username: $username",);

	$ch = curl_init();
	curl_setopt( $ch, CURLOPT_URL, $url );
	curl_setopt( $ch, CURLOPT_POST, 1 );
	curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
	curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
	curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $post_fields ) );
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

	$result = curl_exec( $ch );

	if ( curl_errno( $ch ) !== 0 ) {
		echo 'Error:' . curl_error($ch);
	}

	curl_close( $ch );

확실히 실수를 한 것 같은데, 무엇이 잘못되었는지 모르겠습니다. 도와주실 수 있을까요?

감사합니다

코드를 공유해 주셔서 감사합니다. 전체 코드를 확인하지 않고서는 완전히 디버깅하기가 어렵습니다. 스크립트 외부에서 API를 조회하는 것을 권장합니다. 예를 들어, curl을 사용할 수 있습니다.

curl -X GET "https://mydiscourse.com/posts.json" \
-H "Api-Key: 714552c6148e1617aeab526d0606184b94a80ec048fc09894ff1a72b740c5f19" \
-H "Api-Username: michael"

명령줄이나 curl 사용에 익숙하지 않다면, Postman을 사용하여 API 문제를 디버깅하는 것을 권장합니다. Postman은 개발자로서 API를 테스트하거나 구축할 때 저에게 필수적인 도구입니다.

아래는 Discourse 라우트가 이미 설정된 Postman 워크스페이스입니다. API 키와 사용자 이름만 설정하면 됩니다.
Discourse API Documentation Postman API Network