Flarum api 创建帖子
要使用Flarum API创建帖子,您需要进行以下步骤:
-
获取访问令牌:首先,您需要获取Flarum的访问令牌。可以通过向Flarum发送POST请求来获取访问令牌。请求的URL应为
/api/token,并且需要在请求头中包含您的用户名和密码。示例代码(使用Python的requests库):
import requests url = 'https://your-flarum-url/api/token' headers = {'Content-Type': 'application/json'} data = { 'identification': 'your-username', 'password': 'your-password' } response = requests.post(url, headers=headers, json=data) access_token = response.json()['token'] -
创建帖子:使用获取到的访问令牌,您可以发送POST请求来创建帖子。请求的URL应为
/api/posts,并且需要在请求头中包含访问令牌。示例代码(使用Python的requests库):
import requests url = 'https://your-flarum-url/api/posts' headers = { 'Content-Type': 'application/json', 'Authorization': f'Bearer {access_token}' } data = { 'data': { 'type': 'posts', 'attributes': { 'content': 'Your post content' }, 'relationships': { 'discussion': { 'data': { 'type': 'discussions', 'id': 'discussion-id' } } } } } response = requests.post(url, headers=headers, json=data)在上面的代码中,您需要将
discussion-id替换为您要将帖子添加到的讨论的ID,并将Your post content替换为您要发布的帖子内容。
请注意,Flarum API还提供其他功能,例如更新帖子、删除帖子等。您可以根据自己的需求进行相应的调整。
原文地址: https://www.cveoy.top/t/topic/i3hK 著作权归作者所有。请勿转载和采集!