简单=核心逻辑安装并启用WordPress插件:JWT Authentication for WP REST API启用wordpress服务器HTTP:Authorization
<用途:允许呼叫/wp-json/jwt-auth/v1
api添加JWT_AUTH_SECRET_KEY
进入wp-config.php
拨打电话POST https://www.yourWebsite.com/wp-json/jwt-auth/v1/token
使用wordpressusername
和password
, 响应包含您的预期jwt token<看起来像:eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczpcL1wvd3d3LmNyaWZhbi5jb20iLCJpYXQiOjE1ODQxMDk4OTAsIm5iZiI6MTU4NDEwOTg5MCwiZXhwIjoxNTg0NzE0NjkwLCJkYXRhIjp7InVzZXIiOnsiaWQiOiIxIn19fQ.YvFzitTfIZaCwtkxKTIjP715795OMAVQowgnD8G3wk0
详细信息请参考我的回答以获取另一篇帖子:Publish WordPress Post with Python Requests and REST API - Stack Overflow调用wordpress REST api创建帖子的python代码: 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
<完整(&L);最新代码请参考: