LoginSignup
7
7

More than 5 years have passed since last update.

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

Last updated at Posted at 2017-07-13

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

7
7
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7