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 を参照してください。

「いいね!」 2

こんにちは、@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

「いいね!」 3