LoginSignup
10
10

More than 5 years have passed since last update.

Wunderlist WebAPIをPHPで叩いてみる

Posted at

とりあえずゴール

  • 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
    2. DESCRIPTION: This is awesome app!!
    3. APP URL: http://example.com
    4. 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
    2. redirect_url: http%3A%2F%2Fexample.com
    3. state: 適当な文字列
  2. 問題なく自分のwunderlistの認証画面に行ったのでaccept
  3. http://example.com/?state=XXX8&code=XXX
  4. code ゲット

access_token取得

  1. https://www.wunderlist.com/oauth/access_token
    1. このAPIに以下のパラメータをjsonにしてくっつけてPOSTで送る
      1. client_id
      2. client_secret
      3. code
  2. とりあえずcurlする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
$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
  2. GET a.wunderlist.com/api/v1/lists/:idこれ叩く
<?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"}"

10
10
0

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
10
10