Publish WordPress Post with Python Requests and REST API
How to get jwt token
?
prerequisite
installed jwt plugin
install and enable WordPress plugin:
wordpress server enable jwt authorization
for example, my wordpress server is: CentOS
- edit
.htaccess
in wordpress root folder
added:
RewriteEngine on
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]
- edit
wp-config.php
add:
define('JWT_AUTH_SECRET_KEY', 'anyValueIsOk');
anyValueIsOk
: change to yours, any value is OK- just do NOT tell other, ^_^
- Remember
service php-fpm restart
after edit php file
POST /wp-json/jwt-auth/v1/token
to generate jwt token
call REST api to generate jwt token
POST https://www.yourWebsite.com/wp-json/jwt-auth/v1/token
{
"username": "wordpress_username",
"password": "wordpress_password"
}
wordpress_username
: here is youradmin
wordpress_password
: here is your123456
response:
{
"token": "eyJ0eXAiOxxxxxxxxxxxxHYBhtuzc",
"user_email": "xxx@yyy.com",
"user_nicename": "xxx",
"user_display_name": "xxx"
}
eyJ0eXAiOxxxxxxxxxxxxHYBhtuzc
: is the jwt token, which is what your need
[Optional] validate token is valid
POST https://www.crifan.com/wp-json/jwt-auth/v1/token/validate
headers:
Authorization: Bearer eyJ0eXAiOxxxxxxxxxxxxHYBhtuzc
response:
{
"code": "jwt_auth_valid_token",
"data": {
"status": 200
}
}
Additionally: use python call wordpress REST api to create posts
code:
import requests
your_jwt_token = "xxx"
curHeaders = {
"Authorization": "Bearer %s" % your_jwt_token,
"Content-Type": "application/json",
"Accept": "application/json",
}
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, # '<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>'
# "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
}
yourHost = 'https://www.crifan.com'
createPostUrl = yourHost + "/wp-json/wp/v2/posts" # 'https://www.crifan.com/wp-json/wp/v2/posts'
resp = requests.post(
createPostUrl,
# proxies=self.requestsProxies,
headers=curHeaders,
# data=json.dumps(postDict),
json=postDict, # internal auto do json.dumps
)
response example:
{
"id": 70410,
"date": "2020-02-27T21:11:49",
"date_gmt": "2020-02-27T13:11:49",
"guid": {
"rendered": "https://www.crifan.com/?p=70410",
"raw": "https://www.crifan.com/?p=70410"
},
"modified": "2020-02-27T21:11:49",
"modified_gmt": "2020-02-27T13:11:49",
"password": "",
"slug": "mac_pip_change_source_server_to_spped_up_download",
"status": "draft",
"type": "post",
"link": "https://www.crifan.com/?p=70410",
"title": {
'raw": "【已解决】Mac中给pip更换源以加速下载",
"rendered": "【已解决】Mac中给pip更换源以加速下载"
},
"content": {
...
}
}
Note:
- latest full code:
- wordpress official doc: Create a Post
0 Comments