Skip to content

Instantly share code, notes, and snippets.

@brunnogomes
Created March 22, 2012 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brunnogomes/2161583 to your computer and use it in GitHub Desktop.
Save brunnogomes/2161583 to your computer and use it in GitHub Desktop.
Redu PHP Client with OAuth
<?php
// this script requires the instalation of the Oauth library
// http://br.php.net/manual/en/book.oauth.php
$api_url = 'http://www.redu.com.br/api';
$authorize_url = 'http://www.redu.com.br/oauth/authorize';
$request_token_url = 'http://www.redu.com.br/oauth/request_token';
$access_token_url = 'http://www.redu.com.br/oauth/access_token';
$consumer_key = 'PUT_YOUR_CONSUMER_KEY_HERE';
$consumer_secret = 'PUT_YOUR_CONSUMER_SECRET_HERE';
session_start();
// In state=1 the next request should include an oauth_token.
// If it doesn't go back to 0
if(!isset($_GET['oauth_token']) && $_SESSION['state']==1) {
$_SESSION['state'] = 0;
}
try {
$oauth = new OAuth($consumer_key,
$consumer_secret,
OAUTH_SIG_METHOD_HMACSHA1,
OAUTH_AUTH_TYPE_URI);
$oauth->enableDebug();
if(!isset($_GET['oauth_token']) && !$_SESSION['state']) {
$request_token_info = $oauth->getRequestToken($request_token_url);
$_SESSION['secret'] = $request_token_info['oauth_token_secret'];
$_SESSION['state'] = 1;
header('Location: '.$authorize_url.'?oauth_token='.$request_token_info['oauth_token']);
exit;
} else if($_SESSION['state']==1) {
$oauth->setToken($_GET['oauth_token'],$_SESSION['secret']);
$access_token_info = $oauth->getAccessToken($access_token_url);
$_SESSION['state'] = 2;
$_SESSION['token'] = $access_token_info['oauth_token'];
$_SESSION['secret'] = $access_token_info['oauth_token_secret'];
}
$oauth->setToken($_SESSION['token'],$_SESSION['secret']);
// fetching Redu Environments
$oauth->fetch("$api_url/environments.json");
$json = json_decode($oauth->getLastResponse());
var_dump($json);
} catch(OAuthException $E) {
var_dump($E);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment