POST /wp/v2/posts 意为要用post方法提交到 /wp/v2/posts这个地址
默认是只读api要实现提交数据需要安装插件jwt,安装了jwt后可以请求到token了,在rest api中传入token信息,系统就不会拒绝你的发布文章的操作了
# encoding:utf-8
import re
import requests
import json
import time
from email.utils import formatdate
from numpy import *
from bs4 import BeautifulSoup
import bs4
import sys
reload(sys)
sys.setdefaultencoding('utf8')
def get_token():
session = requests.Session()
url = 'http://www.jhcms.net/index.php/wp-json/jwt-auth/v1/token'
data = {
'username':"121852835@qq.com",
'password':"yourpassword"
}
headers = {'user-agent': 'Mozolla/5.0'}
resp = session.post(url, data=data, headers=headers, timeout=3335) # 请求
r = json.loads(resp.content, 'utf-8')
return r
if __name__ == '__main__':
r = get_token()
print r
# encoding:utf-8
import re
import requests
import json
import time
import token
from email.utils import formatdate
from numpy import *
from bs4 import BeautifulSoup
import bs4
import sys
reload(sys)
sys.setdefaultencoding('utf8')
def _do_post( token =''):
session = requests.Session()
url = 'http://www.jhcms.net/index.php/wp-json/wp/v2/posts'
data = {
'date': time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()),
'date_gmt': time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime()),
'slug': 'xx',
'status': 'publish',
'password': '',
'title': 'rest api发布post测试',
'content': '系统测试我想我是海冬天的大海',
'author': '121852835@qq.com',
'excerpt': '',
'featured_media': '0',
'comment_status': 'open',
'ping_status': 'closed',
'format': 'standard',
'meta': [],
'sticky': False, # 置顶
'template': '',
'categories': '1', # 1 未分类
'tags': ''
}
headers = {'user-agent': 'Mozolla/5.0',
'Authorization': 'Bearer ' + token
}
resp = session.post(url, data=data, headers=headers, timeout=3335) # 请求
print resp.text
# r = json.loads(resp.content, 'utf-8')
# if r["code"] == 400:
# print r["code"]
# print r["message"]
# print r["data"]
# print r["data"]["status"]
#
#
# # print r["data"]["params"]
# for key in r["data"]["params"]:
# print ("%s=> %s" % (key, r["data"]["params"][key]))
# # print 'resp.text=>' + resp.text
#
# # print time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
# # print time.strftime('%a, %d %b %Y %H:%M:%S GMT+0800 (CST)',time.localtime(time.time())),
# dt = formatdate(None, usegmt=True)
# dt1 = formatdate(None, usegmt=False)
# dt3 = formatdate()
# print(dt)
# print(dt1)
# else:
# print r["code"]
# print r["message"]
# print resp.status_code
if __name__=='__main__':
r = token.get_token()
print r
_do_post(r['token'])