WordPress Rest API 如何使用 python 发布内容

问题描述 投票:0回答:1

所以我正在尝试使用我的 python 脚本通过 wordpress rest api 发布内容。然而,我面临的问题是,当我发出我的发帖请求时,我收到了 json 格式的响应,所有已经存在于网站上的帖子,而不是创建一个新帖子。有人可以在这里支持吗?我的代码如下所示;

import requests import json import base64

url = "https://myurl.com/wp-json/wp/v2/posts" 
user = "Username" 
password = "application password" 
credentials = user + ':' + password 
token = base64.b64encode(credentials.encode()) 
header = {'Authorization': 'Basic ' + token.decode('utf-8')} 
post = {  'title'    : 'Test WP-API',   
          'content'  : 'This is my first post created using restAPI',  
          'status'   : 'publish', 
} 
responce = requests.post(url , headers=header, json=post)

print(responce.json())

我试图在任何地方用谷歌搜索这个问题,但似乎没有人遇到这个问题。如果有人能支持,我将不胜感激。

python api wordpress-rest-api
1个回答
0
投票

我偶然发现了同样的问题。据我所知,我通过设置作者(整数索引!)并提供一个 slug 来解决它。 (虽然不确定你是否可以发布。恕我直言,状态应该是“已发布”,而不是“已发布”)

data = {
        'title': 'Test WP API',
        'status': 'draft',
        'slug': 'test-wp-api',
        'author': 1,
        'content': 'This is my first post created using REST-API'
    }
© www.soinside.com 2019 - 2024. All rights reserved.