WP REST APIでBasic認証使って投稿をPOSTして、DELETEまでしてみる
いい加減POST系のリクエストも使えるようにならんとなと思ったので、触り始めてみました。 Basic認証のプラグイン 今回はhttps://github.com/WP-API/Basic-Authを使用します。 $ cd […]
広告ここから
広告ここまで
目次
いい加減POST系のリクエストも使えるようにならんとなと思ったので、触り始めてみました。
Basic認証のプラグイン
今回はhttps://github.com/WP-API/Basic-Authを使用します。
$ cd /PATH/TO/WORDPRESS/wp-content/plugins $ git clone [email protected]:WP-API/Basic-Auth.git $ wp plugin activate Basic-Auth
curlでPOSTリクエストを送る
WP_USER_NAMEとWP_USER_PASSWORDはWordPressへのログイン情報を使用します。
$ curl --user WP_USER_NAME:WP_USER_PASSWORD https://example.com/wp-json/wp/v2/posts -XPOST | jq . % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 155 0 155 0 0 64 0 --:--:-- 0:00:02 --:--:-- 64 { "code": "empty_content", "message": "本文、タイトル、抜粋が空欄です。", "data": { "status": 400 } }
投稿を作成する
curlの場合は以下のようにして記事情報をPOSTします。
$ curl --user WP_USER_NAME:WP_USER_PASSWORD https://example.com/wp-json/wp/v2/posts -XPOST -d 'title=title' -d 'content=sample content' -d 'slug=api-sample'
成功した場合は、以下のようなレスポンスが返ってきます。
{ "id": 3791, "date": "2016-09-14T21:15:25", "date_gmt": null, "guid": { "rendered": "https://wp-kyoto.cdn.rabify.me/?p=3791", "raw": "https://wp-kyoto.cdn.rabify.me/?p=3791" }, "modified": "2016-09-14T21:15:25", "modified_gmt": null, "password": "", "slug": "api-sample", "status": "draft", "type": "post", "link": "https://wp-kyoto.cdn.rabify.me/?p=3791", "title": { "raw": "title", "rendered": "title" }, "content": { "raw": "sample content", "rendered": "<p>sample content</p>" }, "excerpt": { "raw": "", "rendered": "<p>sample content</p>" }, "author": 1, "featured_media": 0, "comment_status": "open", "ping_status": "closed", "sticky": false, "format": "standard", "categories": [ 1 ], "tags": [], "_links": { "self": [ { "href": "https://wp-kyoto.cdn.rabify.me/wp-json/wp/v2/posts/3791" } ], "collection": [ { "href": "https://wp-kyoto.cdn.rabify.me/wp-json/wp/v2/posts" } ], "about": [ { "href": "https://wp-kyoto.cdn.rabify.me/wp-json/wp/v2/types/post" } ], "author": [ { "embeddable": true, "href": "https://wp-kyoto.cdn.rabify.me/wp-json/wp/v2/users/1" } ], "replies": [ { "embeddable": true, "href": "https://wp-kyoto.cdn.rabify.me/wp-json/wp/v2/comments?post=3791" } ], "version-history": [ { "href": "https://wp-kyoto.cdn.rabify.me/wp-json/wp/v2/posts/3791/revisions" } ], "wp:attachment": [ { "href": "https://wp-kyoto.cdn.rabify.me/wp-json/wp/v2/media?parent=3791" } ], "wp:term": [ { "taxonomy": "category", "embeddable": true, "href": "https://wp-kyoto.cdn.rabify.me/wp-json/wp/v2/categories?post=3791" }, { "taxonomy": "post_tag", "embeddable": true, "href": "https://wp-kyoto.cdn.rabify.me/wp-json/wp/v2/tags?post=3791" } ], "curies": [ { "name": "wp", "href": "https://api.w.org/{rel}", "templated": true } ] } }
POSTした記事を見る
デフォルトでは下書き(draft)として保存されているので、GET /wp/v2/posts/{POST_ID}
だけだと以下のようにエラーになります。
$ curl https://example.com/wp-json/wp/v2/posts/3791 | jq . % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 168 0 168 0 0 176 0 --:--:-- --:--:-- --:--:-- 176 { "code": "rest_forbidden", "message": "その操作を実行する権限がありません。", "data": { "status": 403 } }
ここも認証情報をつけて送りましょう。
$ curl --user WP_USER_NAME:WP_USER_PASSWORD https://example.com/wp-json/wp/v2/posts/3791 | jq . { "id": 3791, "date": "2016-09-14T21:15:25", "date_gmt": null, "guid": { "rendered": "https://wp-kyoto.cdn.rabify.me/?p=3791" }, "modified": "2016-09-14T21:15:25", "modified_gmt": null, "slug": "api-sample", "type": "post", "link": "https://wp-kyoto.cdn.rabify.me/?p=3791", "title": { "rendered": "title" }, "content": { "rendered": "<p>sample content</p>" }, "excerpt": { "rendered": "<p>sample content</p>" }, "author": 1, "featured_media": 0, "comment_status": "open", "ping_status": "closed", "sticky": false, "format": "standard", "categories": [ 1 ], "tags": [], "_links": { "self": [ { "href": "https://wp-kyoto.cdn.rabify.me/wp-json/wp/v2/posts/3791" } ], "collection": [ { "href": "https://wp-kyoto.cdn.rabify.me/wp-json/wp/v2/posts" } ], "about": [ { "href": "https://wp-kyoto.cdn.rabify.me/wp-json/wp/v2/types/post" } ], "author": [ { "embeddable": true, "href": "https://wp-kyoto.cdn.rabify.me/wp-json/wp/v2/users/1" } ], "replies": [ { "embeddable": true, "href": "https://wp-kyoto.cdn.rabify.me/wp-json/wp/v2/comments?post=3791" } ], "version-history": [ { "href": "https://wp-kyoto.cdn.rabify.me/wp-json/wp/v2/posts/3791/revisions" } ], "wp:attachment": [ { "href": "https://wp-kyoto.cdn.rabify.me/wp-json/wp/v2/media?parent=3791" } ], "wp:term": [ { "taxonomy": "category", "embeddable": true, "href": "https://wp-kyoto.cdn.rabify.me/wp-json/wp/v2/categories?post=3791" }, { "taxonomy": "post_tag", "embeddable": true, "href": "https://wp-kyoto.cdn.rabify.me/wp-json/wp/v2/tags?post=3791" } ], "curies": [ { "name": "wp", "href": "https://api.w.org/{rel}", "templated": true } ] } }
管理画面から見る
元のリクエスト内容に含まれていたtitle
content
slug
が反映されてるのがわかります。
作った下書きを削除する
作った記事は消しちゃいましょう。
DELETE wp/v2/posts/{POST_ID}
で消せます。
$ curl --user WP_USER_NAME:WP_USER_PASSWORD https://example.com/wp-json/wp/v2/posts/3791 -XDELETE | jq . % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 3632 0 3632 0 0 1137 0 --:--:-- 0:00:03 --:--:-- 1137 { "id": 3791, "date": "2016-09-14T21:15:25", "date_gmt": null, "guid": { "rendered": "https://wp-kyoto.cdn.rabify.me/?p=3791", "raw": "https://wp-kyoto.cdn.rabify.me/?p=3791" }, "modified": "2016-09-14T21:15:25", "modified_gmt": null, "password": "", "slug": "api-sample", "status": "draft", "type": "post", "link": "https://wp-kyoto.cdn.rabify.me/?p=3791", "title": { "raw": "title", "rendered": "title" }, "content": { "raw": "sample content", "rendered": "<p>sample content</p>" }, "excerpt": { "raw": "", "rendered": "<p>sample content</p>" }, "author": 1, "featured_media": 0, "comment_status": "open", "ping_status": "closed", "sticky": false, "format": "standard", "categories": [ 1 ], "tags": [], "_links": { "self": [ { "href": "https://wp-kyoto.cdn.rabify.me/wp-json/wp/v2/posts/3791" } ], "collection": [ { "href": "https://wp-kyoto.cdn.rabify.me/wp-json/wp/v2/posts" } ], "about": [ { "href": "https://wp-kyoto.cdn.rabify.me/wp-json/wp/v2/types/post" } ], "author": [ { "embeddable": true, "href": "https://wp-kyoto.cdn.rabify.me/wp-json/wp/v2/users/1" } ], "replies": [ { "embeddable": true, "href": "https://wp-kyoto.cdn.rabify.me/wp-json/wp/v2/comments?post=3791" } ], "version-history": [ { "href": "https://wp-kyoto.cdn.rabify.me/wp-json/wp/v2/posts/3791/revisions" } ], "wp:attachment": [ { "href": "https://wp-kyoto.cdn.rabify.me/wp-json/wp/v2/media?parent=3791" } ], "wp:term": [ { "taxonomy": "category", "embeddable": true, "href": "https://wp-kyoto.cdn.rabify.me/wp-json/wp/v2/categories?post=3791" }, { "taxonomy": "post_tag", "embeddable": true, "href": "https://wp-kyoto.cdn.rabify.me/wp-json/wp/v2/tags?post=3791" } ], "curies": [ { "name": "wp", "href": "https://api.w.org/{rel}", "templated": true } ] }
あまり消せた感ないなーという気はしますが、ちゃんと消えてます。
もう一度GETしてslugを見ると、以下のように__trashed
がついているのがわかります。
$ curl --user WP_USER_NAME:WP_USER_PASSWORD https://example.com/wp-json/wp/v2/posts/3791 | jq .slug "api-sample__trashed"
あとはこの辺のリクエスト処理をbashでやるなり、PythonやJSでやるなり自由にするといいかなと思います。