TwitterKitではじめる OAuthスピードクッキング

4,800 views
4,540 views

Published on

第2回CakePHP勉強会@福岡のLT発表資料です

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
4,800
On SlideShare
0
From Embeds
0
Number of Embeds
943
Actions
Shares
0
Downloads
14
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide






























  • TwitterKitではじめる OAuthスピードクッキング

    1. 1. Jul 2010 CakePHP
    2. 2. • Twitter API • OAuth • TwitterKit • TwitterKit • TwitterKit
    3. 3. About Me • WEB Developer / PHP / CakePHP / HTML / CSS / jQuery / PostgreSQL / MySQL / iPhone / / / / / / / / no more • BLOG: http://php-tips.com/ • TWITTER: @nojimage
    4. 4. Twitter API
    5. 5. Twitter API • http://api.twitter.com/ HTTP JSON XML ‣ ‣ ‣ ‣ etc... • • http://dev.twitter.com/
    6. 6. OAuth • Twitter API Basic OAuth ‣ Basic • ConsumerKey AccessToken ‣ ‣ Twitter AccessToken • dev.twitter.com
    7. 7. OAuth • Twitter ‣ ConsumerKey RequestToken Twitter ‣ Authorize URL ← • Twitter • Twitter ‣ URL (oauth_token, oauth_verifier) ‣ RequestToken AccessToken Twitter ‣ AccessToken ConsumerKey API
    8. 8. TwitterKit
    9. 9. TwitterKit • TwitterAPI ‣ DataSource, Behavior, Component ‣ Twitter • ‣ PHP 5.2 upper (json_encode/json_decode) ‣ CakePHP 1.3 + ‣ jQuery 1.4.2 + http://github.com/elstc/twitter_kit
    10. 10. TwitterKit • Twitter API ‣ http://openlist.jp/ ‣ http://tsuketter.com/ ( ) • pear HTTP_OAuth ‣ pear Neil Crookess(@neilcrookes) HttpSocketOauth • OAuth
    11. 11. • •
    12. 12. • •
    13. 13. • http://dev.twitter.com/apps/new ‣ ‣ callback localhost ‣ Read Write
    14. 14. • http://dev.twitter.com/apps/{app_id} • Consumer Key Consumer Secret • My Access Token AccessToken
    15. 15. TwitterKit Install • [git clone] cd app/plugins git clone http://github.com/elstc/twitter_kit.git ‣ git github • DB [cake schema] cake/console/cake schema create TwitterKit.TwitterKit ‣ Enter
    16. 16. database.php • ConsumerKey, ConsumerSecret, CallbackURL <?php class DATABASE_CONFIG { var $default = array( // DB ..... ); var $twitter = array( // twitter 'driver' => 'TwitterKit.TwitterSource', 'oauth_consumer_key' => '{YOUR_CONSUMER_KEY}', 'oauth_consumer_secret' => '{YOUR_CONSUMER_SECRET}', 'oauth_callback' => '/twitter_kit/oauth/callback', 'cache' => true, // app/tmp/cache/twitter/ ); }
    17. 17. Model • Behavior <?php class Tiwt extends AppModel { var $name = 'Tiwt'; var $useTable = false; // // TwitterTweetBehavior var $actsAs = array('TwitterKit.TwitterTweet'); }
    18. 18. Controller • Component, Helper <?php class TiwtsController extends AppController { var $name = 'Tiwts'; // var $components = array('TwitterKit.Twitter'); // TwitterFormHelper var $helpers = array('TwitterKit.TwitterForm'); }
    19. 19. AuthComponent • AppController::beforeFilter() <?php class AppController extends Controller { var $components = array('Session', 'Auth'); // ... public function beforeFilter() { $this->Auth->authorize = 'controller'; $this->Auth->userModel = 'TwitterKit.TwitterKitUser'; $this->Auth->loginAction = array( 'plugin' => 'twitter_kit', 'controller' => 'users', 'action' => 'login'); } // ...
    20. 20. AuthComponent • isAuthorized AccessToken <?php class AppController extends Controller { // ... public function isAuthorized() { // set OAuth ConnectionManager::getDatasource('twitter')->setToken( $this->Auth->user('oauth_token'), $this->Auth->user('oauth_token_secret') ); // ... return true; } // ...
    21. 21. Action! • TwitController::index() • HOME • Model fetch • TwitController::add() • POST • Model teewt •
    22. 22. TiwtsController::index() function index() { if ($this->Session->read('reflash')) { // $this->Twitter->refreshCache(); } $timelines = $this->Tiwt->fetch($this->params['named']); $this->set(compact('timelines')); }
    23. 23. Tiwt::fetch() function fetch($options = array()) { $params = array(); foreach (array('since_id', 'max_id', 'count', 'page') as $key) { if (!empty($options[$key])) { $params[$key] = $options[$key]; } } // $results = $this->getTwitterSource()->statuses_home_timeline($params); if (empty($results)) { return array(); } // return array_map(array($this, '_reverse'), $results); }
    24. 24. TiwtsController::add() function add() { $this->autoRender = false; if (!empty($this->data)) { if ( $this->Tiwt->teewt($this->data) ) { $this->Session->write('reflash', true); // $this->Session->setFlash($this->Tiwt->strrev(' ')); } else { $this->Session->setFlash($this->Tiwt->strrev(' ')); } } $this->redirect(array('action' => 'index')); }
    25. 25. Tiwt::teewt() function teewt($data = null) { if (empty($data)) { $data = $this->data; } // (tweet TwitterKit::TwitterTweet ) return $this->tweet($this->strrev($data[$this->alias]['text'])); }
    26. 26. Tiwt::strrev(), Tiwt::_reverse() function _reverse($data) { if (empty($data)) { return array(); } $data['rev']['text'] = $this->strrev($data['text']); $data['rev']['user']['screen_name'] = $this->strrev( $data['user']['screen_name']); return $data; } function strrev($str){ preg_match_all('/./us', $str, $ar); return join('', array_reverse($ar[0])); }
    27. 27. View • layout/default.ctp • jQuery • tiwts/index.ctp • → TwitterFormHelper::tweet() •
    28. 28. http://tiwt.php-tips.com/
    29. 29. • Twitter • TwitterKit github PULL

    ×