Last active
December 22, 2015 21:38
-
-
Save NorihikoMoriyama/6534301 to your computer and use it in GitHub Desktop.
CakePHP2でTwitterのOAuthをしてみる[cakephp][twitter]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
App::import('Vendor', 'Oauth', array('file' => 'OAuth'.DS.'OAuthClient.php')); | |
class TwitterApiComponent extends Component { | |
/** | |
* 使用するコンポーネント | |
*/ | |
public $components = array('Session'); | |
/** | |
* Twitter用プロパティ | |
*/ | |
public $consumerKey = '***'; | |
public $consumerSecret = '*****'; | |
public $callback_url = 'http://***/callback/'; | |
public $api_base_url = 'https://api.twitter.com/1.1/'; | |
public $request_token_url = 'https://api.twitter.com/oauth/request_token'; | |
public $authorize_url = 'https://api.twitter.com/oauth/authorize'; | |
public $access_token_url = 'https://api.twitter.com/oauth/access_token'; | |
/** | |
* コントローラ | |
*/ | |
public $_controller; | |
/** | |
* Twitterインスタンス | |
*/ | |
public $_twitter; | |
/** | |
* | |
*/ | |
public function initialize(Controller $controller) | |
{ | |
$this->_controller = $controller; | |
} | |
function startup(Controller $controller) | |
{ | |
$this->_twitter = new OAuthClient($this->consumerKey, $this->consumerSecret); | |
} | |
/** | |
* OAuth Twitter | |
*/ | |
public function twitterOauthLogin() | |
{ | |
//リクエスト用トークンを生成 | |
$requestToken = $this->_twitter->getRequestToken( | |
$this->request_token_url, | |
$this->callback_url | |
); | |
$this->Session->write('twitter_request_token', $requestToken); | |
$url = 'http://twitter.com/oauth/authorize?oauth_token='. $requestToken->key; | |
$this->_controller->redirect($url); | |
} | |
/** | |
* Callback | |
*/ | |
public function twitterOauthCallback() | |
{ | |
$requestToken = $this->Session->read('twitter_request_token'); | |
//アクセストークンの取得 | |
$accessToken = $this->_twitter->getAccessToken($this->access_token_url, $requestToken); | |
$this->Session->write('twitter_access_token', $accessToken); | |
} | |
/** | |
* APIコールメソッド | |
*/ | |
public function api($path, $param=array(), $method="get", $format="json") | |
{ | |
$accessToken = $this->Session->read('twitter_access_token'); | |
if(!$accessToken){ | |
return false; | |
} | |
$url = $this->api_base_url. $path; | |
$method = strtolower($method); | |
$data = $this->_twitter->$method($accessToken->key, $accessToken->secret, $url, $param); | |
if($format == "json"){ | |
return json_decode($data,true); | |
} | |
return $data; | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class TwitterController extends AppController { | |
public $components = array('TwitterApi'); | |
/* | |
* TOP | |
*/ | |
public function index(){ | |
} | |
/* | |
* 認証を開始する | |
*/ | |
public function twitterOauthStart(){ | |
$this->TwitterApi->twitterOauthLogin(); | |
} | |
public function callback(){ | |
//拒否された場合 | |
if(!empty($this->request->query['denied'])){ | |
$this->render('index'); | |
return; | |
} | |
$this->TwitterApi->twitterOauthCallback(); | |
//Twitterプロフィール情報の取得 | |
$path = "account/verify_credentials.json"; | |
$res_profile = $this->TwitterApi->api($path); | |
//自動フォロー | |
$friendships = 'pad_sexy'; | |
$param = array('screen_name' => $friendships); | |
$path = "friendships/create.json"; | |
$method = "post"; | |
$res_follow = $this->TwitterApi->api($path, $param, $method); | |
//Tweet | |
$tweet = "TEST"; | |
$param = array('status' => $tweet); | |
$path = "statuses/update.json"; | |
$method = "post"; | |
$res_tweet = $this->TwitterApi->api($path, $param, $method); | |
$this->redirect(array('controller'=> 'twitter', 'action'=>'thankyou')); | |
exit; | |
} | |
/* | |
* 完了画面 | |
*/ | |
public function thankyou() { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment