- 安装鉴权插件: WordPress REST API Authentication
- 鉴权面板选择 JWT Authentication
get_token
def get_token():
url = "[http://kh.baidu.com/wp-json/api/v1/token](http://kh.baidu.com/wp-json/api/v1/token)"
payload = {'username': 'yuwei', 'password': '!eeHr1R%wey'}
response = requests.post(url, data = payload)
return "Bearer "+response.json()["jwt_token"]
update_media
def upload_media(thumb):
global headers
r = requests.get(thumb)
image = f"{uuid1().hex}.jpg"
f = open(image, "wb+")
f.write(r.content)
f.seek(0)
# 上传图片
url = "[http://kh.baidu.com/wp-json/wp/v2/media](http://kh.baidu.com/wp-json/wp/v2/media)"
response = requests.post(url, headers=headers, files={"file":f})
f.close()
os.remove(image)
return response.json()["id"]
post
def posts(_id, title, content, release_time, category, featured_media):
global headers, channel
url = "[http://kh.baidu.com/wp-json/wp/v2/posts](http://kh.baidu.com/wp-json/wp/v2/posts)"
data = {
"title":title,
"content":content+f'from: <a href="[https://baidu.com/km/m/detail/article/{_id}](https://baidu.com/km/m/detail/article/{_id})">[https://baidu.com/km/m/detail/article/{_id}](https://baidu.com/km/m/detail/article/{_id})</a>',
"categories": [channel.get(category, 1)],
"featured_media": featured_media,
"date": release_time.strftime("%Y-%m-%d %H:%M:%S"),
"status": "publish"
}
print(data)
r = requests.post(url, json=data, headers=headers)