@Grab(group='org.apache.httpcomponents', module='httpclient', version='4.1.2') import org.apache.http.* import org.apache.http.client.* import org.apache.http.client.entity.* import org.apache.http.client.methods.* import org.apache.http.impl.client.* import org.apache.http.message.* import org.apache.http.protocol.* import groovy.json.* httpclient = new DefaultHttpClient() // ログイン method = new HttpPost("http://localhost/wp-login.php") values = [ new BasicNameValuePair("log", "admin"), new BasicNameValuePair("pwd", "admin"), new BasicNameValuePair("redirect_to", "http://localhost/"), new BasicNameValuePair("testcookie", "1") ] method.setEntity(new UrlEncodedFormEntity(values, HTTP.UTF_8)) response = httpclient.execute(method) println response.getStatusLine().getStatusCode() println response.getEntity().getContent().text // nonceを取得 method = new HttpGet("http://localhost/?json=get_nonce&" + "method=create_post&controller=posts" ) response = httpclient.execute(method) json = new JsonSlurper().parseText(response.getEntity().getContent().text) println json nonce = json.nonce // 投稿の作成 title="groovyによる投稿" content="groovyによる投稿です。" author="admin" categories="groovy,wordpress" tags="howto" method = new HttpGet("http://localhost/?json=create_post&nonce=${nonce}&" + "status=publish&title=${title}&content=${content}&" + "author=${author}&categories=${categories}&tags=${tags}" ) response = httpclient.execute(method) json = new JsonSlurper().parseText(response.getEntity().getContent().text) println json
動作環境
groovy 1.8.4, JDK6 Update29, Wordpress 3.2.1, JSON API plugin 1.0.7
JSON APIのページ
http://wordpress.org/extend/plugins/json-api/
0 件のコメント:
コメントを投稿