通过API发布新主题

你好,

我似乎无法让发帖功能正常工作,不幸的是,我在 API 文档和论坛上都未能找到太多相关信息——

对于标题和原始帖子正文字符串,我需要做什么类型的编码或清理(如果有的话)?当我使用包含 {api_username, api_key, title, raw, created_at, archetype, skip_validations} 字段的 form-data 头部向 /posts.json 发布时,我奇怪地收到了 404 错误(尝试使用 Python,但 Postman 也得到相同结果)。能否指点一下我哪里做错了?

Standard www-form encoding is all. Make sure you’re sending either (api_username, api_key) or (Cookie, X-CSRF-Token).

5 个赞

But am I supposed to receive 404 in any case? I do include api_username and api _key in the form…

Update: if I send the same request under admin user / master api_key it goes through, but I get 404 if I submit under another user but still with master api_key, bug or my mistake?

Can you provide more info and maybe a code snippet of your posting attempt?

3 个赞

Hello - I don’t want to create a new topic because I seem to be having the exact same issue; I’m using the latest discourse_api gem, and I have generated a master API key; I’m creating posts on behalf of existing users (fetching content they put elsewhere).

A normal call

c.create_topic category: category, title: new_title,
                   raw: new_content, api_username: username

Works on some users, and not on others; I get a {"status"=>404, "error"=>"Not Found"} response.

Only differences I can tell are:

  • The user for whom I can create is in staff (others aren’t)
  • The user for whom I can create has a user name identical to the name, and is lowercase (others don’t and aren’t)

Furthermore, if I go to the web interface, create the post, and transfer ownership, it works. So far I’m letting the automaton do its job with the ones that can be created, and give me the text to manually create the ones that can’t. But at over 1000 posts that won’t be created automatically, I despair a little :sweat_smile:

EDIT: verified name situation is not the issue… It may be related to the staff thing.

EDITEDIT: I worked around this by creating in a user’s name, then changing ownership to another user. The mere fact that this is possible on the same API key makes me lean towards the 404 behavior being a bug.

2 个赞

你解决了吗?

如何在创建帖子 API 中发送图片。因为在文档的 API 请求中没有提到图片的键。
https://docs.discourse.org/#tag/Posts/operation/createTopicPostPM

{
  "title": "string",
  "raw": "string",
  "topic_id": 0,
  "category": 0,
  "target_recipients": "blake,sam",
  "target_usernames": "string",
  "archetype": "private_message",
  "created_at": "string",
  "reply_to_post_number": 0,
  "embed_url": "string",
  "external_id": "string"
}
1 个赞

您必须使用 uploads 端点:\u003chttps://docs.discourse.org/#tag/Uploads\u003e
然后,使用返回的 short_url 值将其插入您的帖子中。

2 个赞