Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
7kai Tasks OAuth sample for limonade (php)
<?php
session_start();
require_once dirname(__FILE__) . '/lib/limonade.php'; # http://limonade-php.github.com/
require_once dirname(__FILE__) . '/lib/OAuth.php'; # http://oauth.googlecode.com/svn/code/php/OAuth.php
define('CONSUMER_KEY', 'YOUR_APP_CONSUMER_KEY');
define('CONSUMER_SECRET', 'YOUR_APP_CONSUMER_SECRET');
define('CALLBACK_URL', 'http://127.0.0.1:5000/callback');
define('REQUEST_URL', 'https://tasks.7kai.org/oauth/request_token');
define('AUTHORIZED_URL', 'https://tasks.7kai.org/oauth/authorize');
define('ACCESS_TOKEN_URL', 'https://tasks.7kai.org/oauth/access_token');
define('API_URL', 'https://tasks.7kai.org/api/1/account/me');
dispatch('/', function() {
$request_token = get_request_token();
$_SESSION['request_token'] = $request_token->to_string();
redirect_to(AUTHORIZED_URL . '?oauth_token=' . $request_token->key);
});
dispatch('/callback', function() {
$request_token = token_from_string($_SESSION['request_token']);
$access_token = get_access_token($request_token, $_GET['oauth_verifier']);
$_SESSION['access_token'] = $access_token->to_string();
return redirect_to('/me');
});
dispatch('/me', function() {
$access_token = token_from_string($_SESSION['access_token']);
$result = get_protected_resource($access_token, 'GET', API_URL, array());
return $result;
});
run();
function token_from_string($string) {
$params = OAuthUtil::parse_parameters($string);
return new OAuthToken($params['oauth_token'], $params['oauth_token_secret']);
}
function get_request_token($consumer, $callback_url) {
return token_from_string(
oauth_request(
NULL,
'POST',
REQUEST_URL,
array('oauth_callback' => CALLBACK_URL)
)
);
}
function get_access_token($request_token, $verifier) {
return token_from_string(
oauth_request(
$request_token,
'POST',
ACCESS_TOKEN_URL,
array('oauth_verifier' => $verifier)
)
);
}
function get_protected_resource($access_token, $method, $url, $params) {
return oauth_request(
$access_token,
$method,
$url,
$params
);
}
function oauth_request($token, $method, $url, $params) {
$consumer = new OAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
$sig_method = new OAuthSignatureMethod_HMAC_SHA1();
$request = OAuthRequest::from_consumer_and_token($consumer, $token, $method, $url, $params);
$request->sign_request($sig_method, $consumer, $token);
$data = $request->to_postdata();
$header = array(
'Content-Type: application/x-www-form-urlencoded',
'Content-Length: ' . strlen($data)
);
return file_get_contents(
$request->get_normalized_http_url(),
false,
stream_context_create(
array(
'http'=> array(
'method' => $request->get_normalized_http_method(),
'header' => implode("\r\n", $header),
'content' => $data
)
)
)
);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment