无法发布主题

尝试使用 API 发布主题时失败,并显示以下错误消息:
{‘action’: ‘create_post’, ‘errors’: [‘Body is too short (minimum is 1 character)’, ‘Body seems unclear, is it a complete sentence?’]}

  • 要发布的消息大约有 25K,是一个新闻列表

  • 手动发布可以成功

  • 但使用 API 发布时会失败

  • 默认帖子大小是 35K(?),我已经(在设置中)增加了它以允许这个更长的消息

  • 如果使用下面的代码发布一个简短的句子,则可以正常工作

  • body min entropy 改为 0,也没有帮助

  • 在论坛上搜索过,没有找到解决方案

  • 有什么想法/建议吗?谢谢…

这是 Python 代码:

import requests

# Authentication headers
headers = {
    "Api-Key": '***',
    "Api-Username": 'newsbot',
}

lines = "some news, around 25K"

# Topic data
topic_data = {
    "title":"latest news",
    "raw": lines,
    "category": 28,
}

# Send a POST request to create the topic
response = requests.post('https://www.mydomain.com/posts.json', json=topic_data, headers=headers)

# Check if the request was successful
if response.status_code == 200:
    print("Topic created successfully!")
    topic_id = response.json().get("id")
    print(f"Topic ID: {topic_id}")
else:
    print(response.json())
    print(f"Failed to create the topic. Status code: {response.status_code}")

顺便说一句,要发布的内容是像这样的新闻列表:

[grid]
![](https://static01.nyt.com/images/2023/10/16/multimedia/animage.jpg)
![](https://content.api.news/v3/images/bin/image.jpg)
[/grid]

<details>
<summary><b>news title</b></summary>
news conentnews conentnews conentnews conentnews
conent news conentnews conentnews conentnews conentnews
conentnews conentnews conentnews conentnews conentnews
conentnews conentnews conentnews conentnews conent
about this long for one news

[The New source 2023-10-16T21:16:45Z](https://www.newssource.com/live/anews)

尝试在 topic_data 中添加 \"skip_validations\": true。(在某些情况下 true 需要用引号括起来,但我不认为 Python 是这种情况。)

skip_validations 参数会让 Discourse 跳过对诸如 min topic title lengthbody min entropy 等站点设置的验证。

3 个赞

谢谢 @simon

试了

"skip_validations": True

它抑制了错误,创建了一个主题,但它是空的,嗯……
怀疑我的帖子内容中的 HTML 语法是否需要编码?

1 个赞

随机猜一下……也许将 Content-Type 设置为 application/json

在 header 或 body (topic_data) 中尝试过,它仍然会创建一个新的空主题帖子。

我在我的实例上尝试了您的 Python 脚本(未指定 content-type),它运行得非常完美:

1 个赞

我不得不问一下,但你的代码是否能处理短文本?你是否检查过你实际向 API 传递了非空内容?

1 个赞

是的,它适用于短内容,但不适用于长内容(已附加),29848 个字符,不是因为长度,\n怀疑可能是某些特殊字符?\n\n感谢您的帮助!\ntest.zip (12.1 KB)

1 个赞

谢谢 @Arkshine
是的,在上面的帖子中已提到……

1 个赞

我试用了你的文本,它奏效了:

你是否用三引号('''\"\"\")将字符串括起来以避免转义问题?

你在 Python 脚本中如何加载帖子的内容(我猜你是从外部源获取内容)?

1 个赞

我这边也是一样;你的内容没有问题。我猜是你在检索/加载数据的方式上出了些问题。:thinking:

1 个赞

嗯……谢谢你的尝试……很有意思……
我像这样从文件中加载了新闻内容……

with open(sys.argv[1]) as f:
    lines = [line for line in f]

print(lines)
print(len(lines))

它正确地获取了文件内容,嗯……

您是否尝试在 open() 中传递 encoding="utf8"?这或许会有帮助。

2 个赞

是的,加载文件内容有些不对劲…… 谢谢…… 虚惊一场…… :slight_smile:

1 个赞

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