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

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

More than 1 year has 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

Why do not you register as a user and use Qiita more conveniently?
  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
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  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