以下是一个示例代码,用于通过API创建发布时显式指定频道ID:
import requests
def create_post(channel_id, content):
url = "https://api.example.com/posts"
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer YOUR_API_TOKEN"
}
payload = {
"channel_id": channel_id,
"content": content
}
response = requests.post(url, headers=headers, json=payload)
if response.status_code == 200:
print("发布成功")
else:
print("发布失败")
# 在默认频道创建发布
default_channel_id = "DEFAULT_CHANNEL_ID"
content = "这是一条测试发布"
create_post(default_channel_id, content)
请将YOUR_API_TOKEN
替换为您的实际API令牌,DEFAULT_CHANNEL_ID
替换为默认频道的ID。然后,调用create_post
函数并传入频道ID和发布内容,即可通过API创建发布并显式指定频道ID。