无法通过API更新主题正文

我确实遵循了 文档,但是,它只更新了主题的标题,而没有更新正文。

我的代码 (Javascript):

async function updateDiscourseTopic(topic_id) {
  const Discourse_API_Key = "Discourse_API_Key";
  const url = `https://discourse.mysite.com/t/-/${topic_id}.json`;

  const payload = {
    title: 'session_1',
    raw: "新的正文(不适用)"
  }

  const response = await fetch(url, {
    method: 'PUT',
    headers: {
      "Api-Key": Discourse_API_Key,
      "Api-Username": "system",
      "Content-Type": "application/json",
    },
    body: JSON.stringify(payload)
  });

}

只是根据代码猜测,但也许是因为你使用了 topic_id 而不是 post_id?因为在 Discourse 中,第一个帖子或多或少就是主题?

3 个赞

@Lilly 说得对。

主题由标题、类别、标签等属性定义。

但内容属于一个帖子

如果要更新标题和内容,需要 2 个请求。

  1. 对主题执行 PUT 请求。

  2. 对第一个帖子执行 PUT 请求。您可以在页面的 HTML 代码中找到帖子的 ID。在 <article> 中查找 data-post-id 属性。
    示例:

    <article id="post_2" aria-label="post #2 by @Lilly" role="region" data-post-id="1288311" data-
    topic-id="264640" data-user-id="127856" class="boxed onscreen-post">
    
3 个赞

Wow,you are right, it works :sparkling_heart:

And I figured out how to retrieve the ID of the first post in a topic using the Discourse API:

async function getFirstPostIdFromOnetopic(topic_id) {
  const Discourse_API_Key = "Discourse_API_Key";
  const url = `https://discourse.mysite.com/t/${topic_id}/posts.json`;

  const response = await fetch(url, {
    method: 'GET',
    headers: {
      "Api-Key": Discourse_API_Key,
      "Api-Username": "system",
      "Content-Type": "application/json",
    }
  });
  const data = await response.json();
  const firstPostId = data.post_stream.posts[0].id;
  
  return firstPostId;
}

Thank you and @Lilly :innocent:

3 个赞

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.