Created
January 25, 2011 05:23
-
-
Save Leechael/794550 to your computer and use it in GitHub Desktop.
The weibo oauth processing controller with li3_oauth.
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 | |
namespace app\controllers; | |
use lithium\net\http\Router; | |
use lithium\storage\Session; | |
use li3_oauth\models\Consumer; | |
class WeiboController extends \lithium\action\Controller { | |
protected function _init () { | |
parent::_init(); | |
Consumer::config(array( | |
'socket' => 'Curl', | |
'host' => 'api.t.sina.com.cn', | |
'oauth_consumer_key' => '', // Put your consumer key here. | |
'oauth_consumer_secret' => '', // Put your consumer secret key here. | |
)); | |
} | |
public function index () { | |
$token = Session::read('oauth.access', array('name' => 'default')); | |
if (empty($token)) { | |
if (!empty($this->request->query['oauth_token'])) { | |
return $this->redirect('Weibo::access'); | |
} | |
return $this->redirect('Weibo::authorize'); | |
} | |
$result = Consumer::post('/account/verify_credentials.json', array(), compact('token')); | |
$decoded = json_decode($result, true); | |
var_dump($decoded); | |
return "\n"; | |
} | |
public function authorize () { | |
$token = Consumer::token('request'); | |
if (is_string($token)) { | |
return $token; | |
} | |
Session::write('oauth.request', $token, array('name' => 'default')); | |
$callback = implode('', array( | |
'http://', | |
$this->request->host, | |
$this->request->env('base'), | |
Router::match('Weibo::access'), | |
)); | |
$url = Consumer::authorize($token) . '&oauth_callback=' . urlencode($callback); | |
$this->redirect($url); | |
} | |
public function access () { | |
$token = Session::read('oauth.request', array('name' => 'default')); | |
$data = $this->request->query; | |
$access = Consumer::token('access', compact('token', 'data')); | |
Session::write('oauth.access', $access, array('name' => 'default')); | |
$this->redirect('Weibo::index'); | |
} | |
public function destroy () { | |
Session::delete('oauth.access', array('name' => 'default')); | |
Session::delete('oauth.request', array('name' => 'default')); | |
$this->redirect(array('Weibo::index')); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment