as @prathamesh patil
say, you missed the authentication
= your jwt token
how to generate jwt token
?
- simple = core logic
- install and enable WordPress plugin: JWT Authentication for WP REST API
- wordpress server enable
HTTP:Authorization
- purpose: allow to call
/wp-json/jwt-auth/v1
api
- purpose: allow to call
- add
JWT_AUTH_SECRET_KEY
intowp-config.php
- call
POST https://www.yourWebsite.com/wp-json/jwt-auth/v1/token
with wordpressusername
andpassword
, response contain your expected jwt token- look like:
eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczpcL1wvd3d3LmNyaWZhbi5jb20iLCJpYXQiOjE1ODQxMDk4OTAsIm5iZiI6MTU4NDEwOTg5MCwiZXhwIjoxNTg0NzE0NjkwLCJkYXRhIjp7InVzZXIiOnsiaWQiOiIxIn19fQ.YvFzitTfIZaCwtkxKTIjP715795OMAVQowgnD8G3wk0
- look like:
- detail
- please refer my answer for another post:
python code to call wordpress REST api to create posts
code:
def createPost(self,
title,
content,
dateStr,
slug,
categoryNameList=[],
tagNameList=[],
status="draft",
postFormat="standard"
):
"""Create wordpress standard post
by call REST api: POST /wp-json/wp/v2/posts
Args:
title (str): post title
content (str): post content of html
dateStr (str): date string
slug (str): post slug url
categoryNameList (list): category name list
tagNameList (list): tag name list
status (str): status, default to 'draft'
postFormat (str): post format, default to 'standard'
Returns:
(bool, dict)
True, uploaded post info
False, error detail
Raises:
"""
curHeaders = {
"Authorization": self.authorization,
"Content-Type": "application/json",
"Accept": "application/json",
}
logging.debug("curHeaders=%s", curHeaders)
categoryIdList = []
tagIdList = []
if categoryNameList:
# ['Mac']
categoryIdList = self.getTaxonomyIdList(categoryNameList, taxonomy="category")
# category nameList=['Mac'] -> taxonomyIdList=[1374]
if tagNameList:
# ['切换', 'GPU', 'pmset', '显卡模式']
tagIdList = self.getTaxonomyIdList(tagNameList, taxonomy="post_tag")
# post_tag nameList=['切换', 'GPU', 'pmset', '显卡模式'] -> taxonomyIdList=[1367, 13224, 13225, 13226]
postDict = {
"title": title, # '【记录】Mac中用pmset设置GPU显卡切换模式'
"content": content, # '<html>\n <div>\n 折腾:\n </div>\n <div>\n 【已解决】Mac Pro 2018款发热量大很烫非常烫\n </div>\n <div>\n 期间,...performance graphic cards\n </li>\n </ul>\n </ul>\n </ul>\n <div>\n <br/>\n </div>\n</html>'
# "date_gmt": dateStr,
"date": dateStr, # '2020-08-17T10:16:34'
"slug": slug, # 'on_mac_pmset_is_used_set_gpu_graphics_card_switching_mode'
"status": status, # 'draft'
"format": postFormat, # 'standard'
"categories": categoryIdList, # [1374]
"tags": tagIdList, # [1367, 13224, 13225, 13226]
# TODO: featured_media, excerpt
}
logging.debug("postDict=%s", postDict)
# postDict={'title': '【记录】Mac中用pmset设置GPU显卡切换模式', 'content': '<html>\n <div>\n 折腾:\n </div>\n <div>\。。。。<br/>\n </div>\n</html>', 'date': '2020-08-17T10:16:34', 'slug': 'on_mac_pmset_is_used_set_gpu_graphics_card_switching_mode', 'status': 'draft', 'format': 'standard', 'categories': [1374], 'tags': [1367, 13224, 13225, 13226]}
createPostUrl = self.apiPosts
resp = requests.post(
createPostUrl,
proxies=self.requestsProxies,
headers=curHeaders,
# data=json.dumps(postDict),
json=postDict, # internal auto do json.dumps
)
logging.info("createPostUrl=%s -> resp=%s", createPostUrl, resp)
isUploadOk, respInfo = crifanWordpress.processCommonResponse(resp)
return isUploadOk, respInfo
- full & latest code please refer:
createPost
in crifanWordpress.py