Press "Enter" to skip to content

使用Python提交WordPress文章

1.插件安装

需要安装一个WordPress插件,名字叫:JWT Authentication for WP-API

2.编写代码

首先你需要获取提交文档的一个token。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import requests
import json
wpBaseURL = "http://localhost"
def getToken(username, password):
response = requests.request(
"POST",
wpBaseURL+"/wp-json/jwt-auth/v1/token",
data={
"username": username,
"password": password
}
)
responseJson = response.json()
token = responseJson['token']
return token
import requests import json wpBaseURL = "http://localhost" def getToken(username, password): response = requests.request( "POST", wpBaseURL+"/wp-json/jwt-auth/v1/token", data={ "username": username, "password": password } ) responseJson = response.json() token = responseJson['token'] return token
import requests
import json


wpBaseURL = "http://localhost"

def getToken(username, password):
    response = requests.request(
    "POST",
    wpBaseURL+"/wp-json/jwt-auth/v1/token",
    data={
        "username": username,
        "password": password
    }
    )
    responseJson = response.json()
    token = responseJson['token']
    return token

然后再用这个token来提交文档

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import requests
import json
import random
from Token import getToken
wpBaseURL = "http://localhost"
def post_creator(token, article):
WP_url = wpBaseURL + "/wp-json/wp/v2/posts"
headers = {
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": "Bearer "+token
}
print(article)
payload = json.dumps(article)
response = requests.request(
"POST",
WP_url,
data=payload,
headers=headers
)
return response.json()
if __name__ == "__main__":
token = getToken("admin", "admin")
article = {
"title": "title..",
"content": "body....",
"status": "publish"
}
result = post_creator(token, article)
print(result)
import requests import json import random from Token import getToken wpBaseURL = "http://localhost" def post_creator(token, article): WP_url = wpBaseURL + "/wp-json/wp/v2/posts" headers = { "Accept": "application/json", "Content-Type": "application/json", "Authorization": "Bearer "+token } print(article) payload = json.dumps(article) response = requests.request( "POST", WP_url, data=payload, headers=headers ) return response.json() if __name__ == "__main__": token = getToken("admin", "admin") article = { "title": "title..", "content": "body....", "status": "publish" } result = post_creator(token, article) print(result)
import requests
import json
import random
from Token import getToken

wpBaseURL = "http://localhost"

def post_creator(token, article):
    WP_url = wpBaseURL + "/wp-json/wp/v2/posts"
    headers = {
    "Accept": "application/json",
    "Content-Type": "application/json",
    "Authorization": "Bearer "+token
    }
    print(article)
    payload = json.dumps(article)
    response = requests.request(
        "POST",
        WP_url,
        data=payload,
        headers=headers
    )

    return response.json()

if __name__ == "__main__":
    token = getToken("admin", "admin")
    article = {
        "title": "title..",
        "content": "body....",
        "status": "publish"
    }
    result = post_creator(token, article)
    print(result)

参考资料:

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注