Wunderlist WebAPIをPHPで叩いてみる

## とりあえずゴール
- Wunderlist WebAPIのGet a specific List API実行

## やるよ
### API Documentation斜め読み
- https://developer.wunderlist.com/documentation
    - 各APIを叩く前にaccess_token取得する必要がある
        - 各APIを叩く際はヘッダーにaccess_tokenを入れる
    - access_token取得の際に必要になるclient_idはアプリケーションを登録して取得
    - アプリケーションを登録したらclient_idを使ってURLにブラウザでアクセス
    - 問題なければ登録したcallback urlにcodeパラメータがついてリダイレクトされる
    - codeを使ってaccess_token取得API叩けばaccess_tokenゲットできる

### appの登録
1. https://developer.wunderlist.com/apps/new
    1. NAME: awesome app
    1. DESCRIPTION: This is awesome app!!
    1. APP URL: http://example.com
    1. CALLBACK URL: http://example.com

### codeの取得
1. https://www.wunderlist.com/oauth/authorize?client_id=ID&redirect_uri=URL&state=RANDOM
    1. client_id: 登録したappのclient_id
    1. redirect_url: http%3A%2F%2Fexample.com
    1. state: 適当な文字列
1. 問題なく自分のwunderlistの認証画面に行ったのでaccept
1. http://example.com/?state=XXX8&code=XXX
1. code ゲット

### access_token取得
1. https://www.wunderlist.com/oauth/access_token
    1. このAPIに以下のパラメータをjsonにしてくっつけてPOSTで送る
        1. client_id
        1. client_secret
        1. code
1. とりあえずcurlするPHPを書いた

```php
<?php
$url = 'https://www.wunderlist.com/oauth/access_token';
$params = [
	'client_id' => 'XXX',
	'client_secret' => 'XXX',
	'code' => 'XXX',
];
$json = json_encode($params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
```

> HTTP/1.1 400 Bad Request

あらま

1. https://developer.wunderlist.com/documentation/concepts/formats を読む

> Content−Type: application/json

なるほど

```php
<?php
$url = 'https://www.wunderlist.com/oauth/access_token';
$header = [
	'Content-Type: application/json',
];
$params = [
	'client_id' => 'XXX',
	'client_secret' => 'XXX',
	'code' => 'XXX',
];
$json = json_encode($params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
```

> {"access_token":"XXXXX"}"

access_token取れた

### 取得系のAPI叩く
1. Document: https://developer.wunderlist.com/documentation/endpoints/list
1. GET a.wunderlist.com/api/v1/lists/:idこれ叩く

```php
<?php
$url = 'https://a.wunderlist.com/api/v1/lists/XXX';
$header = [
	'Content-Type: application/json',
	'X-Access-Token: ',
	'X-Client-ID: ',
];
$params = [
];
$json = json_encode($params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// curl_setopt($ch, CURLOPT_POST, true); // GETだからコメントアウト
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
// curl_setopt($ch, CURLOPT_POSTFIELDS, $json); // パラメータもないのでコメントアウト
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
```
> {"id":XXX,"title":"test","owner_type":"user","owner_id":XXXX,"list_type":"list","public":false,"revision":1,"created_at":"2016-01-07T18:32:10.243Z","created_by_request_id":"XXX","type":"list"}"