APIを使用してDiscourseフォーラムに新しいトピック投稿を追加できません。私のコードは次のとおりです。
function postComment(topicId, comment) {
const url = `${DISCOURSE_API_URL}/t/${topicId}/posts.json`; // このURLが正しいことを確認してください
Logger.log(`Posting to URL: ${url}`);
const headers = {
"Api-Key": DISCOURSE_API_KEY,
"Api-Username": DISCOURSE_API_USERNAME
};
const payload = {
post: {
topic_id: topicId,
raw: comment
},
};
const options = {
method: "post",
contentType: "application/json",
headers: headers,
payload: JSON.stringify(payload),
muteHttpExceptions: true, // 完全なエラーレスポンスをキャプチャするため
};
try {
const response = UrlFetchApp.fetch(url, options);
const jsonResponse = JSON.parse(response.getContentText());
// デバッグのためにレスポンスをログに記録します
Logger.log(`Response Code: ${response.getResponseCode()}`);
Logger.log(`Response Body: ${JSON.stringify(jsonResponse, null, 2)}`);
// 投稿が成功したかどうかを確認します
if (response.getResponseCode() === 200) {
Logger.log(`Posted comment successfully: ${JSON.stringify(jsonResponse, null, 2)}`);
} else {
Logger.log(`Failed to post comment: ${JSON.stringify(jsonResponse, null, 2)}`);
}
return jsonResponse; // APIレスポンスを返します
} catch (error) {
Logger.log(`Error posting comment: ${error}`);
return null; // エラーの場合はnullを返します
}
}
次のレスポンスが得られます。
Info
Response Code: 404
3:35:29 PM
Info
Response Body: {
"errors": [
"The requested URL or resource could not be found."
],
"error_type": "not_found"
}
フォーラムのトピックは存在し、フォーラムのウェブサイトで直接投稿できるにもかかわらずです。
助けてください。