Help us understand the problem. What is going on with this article?

WordPress REST APIを使ってPython で新規投稿する方法

More than 3 years have passed since last update.

Basic認証用のパスワードは「Application Passwords plugin」から発行しています。

コード

wp_rest_api_post.py
#coding: utf-8

import requests
import json

passward = "anCN 2JnY rcEu H2z4 JRi2 T4Fr" # 自分の環境に合わせて変更してください
user_id = "admin" # 自分の環境に合わせて変更してください
end_point_url ="http://xxx.xyz/wp-json/wp/v2/posts" # 自分の環境に合わせて変更してください

p_title = "APIからの投稿"
p_content = "内容のサンプル<br />サンプルあああああああ"
p_status = "draft"

payload = {
            'title': p_title ,
            'content' : p_content ,
            'status' : p_status
            }

headers = {'content-type': "Application/json"}

r = requests.post( end_point_url, data=json.dumps(payload) , headers=headers, auth=(user_id, passward) )
print(r)

参考文献

WordPress REST APIで投稿の取得から新規投稿を行う
Requests: 人間のためのHTTP
Python Authentication
WP REST API

yuyu_rg
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away