Add Telldus Live! authentication example

This commit is contained in:
Micke Prag 2012-01-09 12:37:41 +01:00
parent f58793ca59
commit 6fa59e9ca9
4 changed files with 100 additions and 0 deletions

View file

@ -0,0 +1,19 @@
<?php
session_start();
require_once 'HTTP/OAuth/Consumer.php';
define('PUBLIC_KEY', '');
define('PRIVATE_KEY', '');
define('URL', 'http://api.telldus.net');
define('REQUEST_TOKEN', constant('URL').'/oauth/requestToken');
define('AUTHORIZE_TOKEN', constant('URL').'/oauth/authorize');
define('ACCESS_TOKEN', constant('URL').'/oauth/accessToken');
define('REQUEST_URI', constant('URL').'/xml');
define('BASE_URL', 'http://'.$_SERVER["SERVER_NAME"].dirname($_SERVER['REQUEST_URI']));
define('TELLSTICK_TURNON', 1);
define('TELLSTICK_TURNOFF', 2);

View file

@ -0,0 +1,21 @@
<?php
require_once 'common.php';
$consumer = new HTTP_OAuth_Consumer(constant('PUBLIC_KEY'), constant('PRIVATE_KEY'), $_SESSION['token'], $_SESSION['tokenSecret']);
try {
$consumer->getAccessToken(constant('ACCESS_TOKEN'));
$_SESSION['accessToken'] = $consumer->getToken();
$_SESSION['accessTokenSecret'] = $consumer->getTokenSecret();
header('Location:index.php');
} catch (Exception $e) {
?>
<p>Authorization failed!</p>
<p><a href="index.php">Go back</a></p>
<?php
}

View file

@ -0,0 +1,13 @@
<?php
require_once 'common.php';
$consumer = new HTTP_OAuth_Consumer(constant('PUBLIC_KEY'), constant('PRIVATE_KEY'));
$consumer->getRequestToken(constant('REQUEST_TOKEN'), constant('BASE_URL').'/getAccessToken.php');
$_SESSION['token'] = $consumer->getToken();
$_SESSION['tokenSecret'] = $consumer->getTokenSecret();
$url = $consumer->getAuthorizeUrl(constant('AUTHORIZE_TOKEN'));
header('Location:'.$url);

View file

@ -0,0 +1,47 @@
<?php
require_once 'common.php';
if (isset($_GET['clear'])) {
session_destroy();
header('location:index.php');
exit();
}
if (!isset($_SESSION['accessToken'])) {
?>We have no access token, <a href="getRequestToken.php">connect us</a><?php
exit();
}
?>
<p>We have access!</p>
<p>
In your system, store these values to do requests for this user:<br>
Token: <?php echo $_SESSION['accessToken']; ?><br>
Secret: <?php echo $_SESSION['accessTokenSecret']; ?>
</p>
<p><a href="index.php?clear">Clear the token and restart</a></p>
<p><a href="index.php?listDevices">List users devices</a></p>
<?php
if (isset($_GET['listDevices'])) {
$consumer = new HTTP_OAuth_Consumer(constant('PUBLIC_KEY'), constant('PRIVATE_KEY'), $_SESSION['accessToken'], $_SESSION['accessTokenSecret']);
$params = array(
'supportedMethods' => constant('TELLSTICK_TURNON') | constant('TELLSTICK_TURNOFF'),
);
$response = $consumer->sendRequest(constant('REQUEST_URI').'/devices/list', $params, 'GET');
echo '<pre>';
echo( htmlentities($response->getBody()));
}
?><p><a href="index.php?listClients">List users clients</a></p><?php
if (isset($_GET['listClients'])) {
$consumer = new HTTP_OAuth_Consumer(constant('PUBLIC_KEY'), constant('PRIVATE_KEY'), $_SESSION['accessToken'], $_SESSION['accessTokenSecret']);
$params = array();
$response = $consumer->sendRequest(constant('REQUEST_URI').'/clients/list', $params, 'GET');
echo '<pre>';
echo( htmlentities($response->getBody()));
}