Skip to content

Instantly share code, notes, and snippets.

@aurman
Last active July 4, 2022 13:00
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save aurman/9813340 to your computer and use it in GitHub Desktop.
Save aurman/9813340 to your computer and use it in GitHub Desktop.
SmartThings .php example authenticating and controlling SmartThings API endpoints
<?php
//client id and client secret
$client = '';
$secret = '';
//hardcode the full url to redirect to this file
$url = "";
//STEP 1 - Get Access Code
if(!isset($_REQUEST['code']) && !isset($_REQUEST['access_token']))
{
header( "Location: https://graph.api.smartthings.com/oauth/authorize?response_type=code&client_id=$client&redirect_uri=".$url."&scope=app" ) ;
}
//STEP 2 - Use Access Code to claim Access Token
else if(isset($_REQUEST['code']))
{
$code = $_REQUEST['code'];
$page = "https://graph.api.smartthings.com/oauth/token?grant_type=authorization_code&client_id=".$client."&client_secret=".$secret."&redirect_uri=".$url."&code=".$code."&scope=app";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $page );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POST, 0 );
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$response = json_decode(curl_exec($ch),true);
curl_close($ch);
if(isset($response['access_token']))
{
//Redirect to self with access token for step 3 for ease of bookmarking
header( "Location: ?access_token=".$response['access_token'] ) ;
}
else
{
print "error requesting access token...";
print_r($response);
}
}
//Step 3 - Lookup Endpoint and write out urls
else if(isset($_REQUEST['access_token']))
{
$url = "https://graph.api.smartthings.com/api/smartapps/endpoints/$client?access_token=".$_REQUEST['access_token'];
$json = implode('', file($url));
$theEndpoints = json_decode($json,true);
print "<html><head><style>h3{margin-left:10px;}a:hover{background-color:#c4c4c4;} a{border:1px solid black; padding:5px; margin:5px;text-decoration:none;color:black;border-radius:5px;background-color:#dcdcdc}</style></head><body>";
print "<i>Save the above URL (access_token) for future reference.</i>";
print " <i>Right Click on buttons to copy link address.</i>";
foreach($theEndpoints as $k => $v)
{
//GET SWITCHES
$switchUrl = "https://graph.api.smartthings.com".$v['url']."/switches";
$access_key = $_REQUEST['access_token'];
$ch = curl_init($switchUrl);
curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Authorization: Bearer ' . $access_key ) );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POST, 0 );
$resp = curl_exec($ch);
curl_close($ch);
$respData = json_decode($resp,true);
if(count($respData) > 0) print "<h2>Switches</h2>";
//let's show each of the switches
foreach($respData as $i => $switch)
{
$label = $switch['label'] != "" ? $switch['label'] : "Unlabeled Switch";
print " <h3>$label</h3>";
$onUrl = "https://graph.api.smartthings.com".$v['url']."/switches/".$switch['id']."/on?access_token=".$_REQUEST['access_token'];
print "<a target='cmd' href='$onUrl'>On</a>";
$offUrl = "https://graph.api.smartthings.com".$v['url']."/switches/".$switch['id']."/off?access_token=".$_REQUEST['access_token'];
print "<a target='cmd' href='$offUrl' value='Off'>Off</a>";
$toggleUrl = "https://graph.api.smartthings.com".$v['url']."/switches/".$switch['id']."/toggle?access_token=".$_REQUEST['access_token'];
print "<a target='cmd' href='$toggleUrl'>Toggle</a><BR>";
}
//GET LOCKS
$lockUrl = "https://graph.api.smartthings.com".$v['url']."/locks";
$access_key = $_REQUEST['access_token'];
$ch = curl_init($lockUrl);
curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Authorization: Bearer ' . $access_key ) );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POST, 0 );
$resp = curl_exec($ch);
curl_close($ch);
$respData = json_decode($resp,true);
if(count($respData) > 0) print "<h2>Locks</h2>";
//let's show the locks
foreach($respData as $i => $lock)
{
$label = $lock['label'] != "" ? $lock['label'] : "Unlabeled Lock";
print "<h3>$label</h3>";
$lockUrl = "https://graph.api.smartthings.com".$v['url']."/locks/".$lock['id']."/lock?access_token=".$_REQUEST['access_token'];
print "<a target='cmd' href='$lockUrl'>Lock</a>";
$unlockUrl = "https://graph.api.smartthings.com".$v['url']."/locks/".$lock['id']."/unlock?access_token=".$_REQUEST['access_token'];
print "<a target='cmd' href='$unlockUrl' value='Off'>Unlock</a><BR>";
}
print "<BR><hr><BR>";
}
//all links in the html document are targeted at this iframe
print "<iframe name='cmd' style='display:none'></iframe></body></html>";
}
?>
@sean1279
Copy link

sean1279 commented Jun 23, 2016

I am not able to get your code working....error requesting access token

@SunboX
Copy link

SunboX commented Jan 6, 2018

If you are located in UK or Europe you may change the API url from

https://graph.api.smartthings.com

to (or similar [1] or [2])

https://graph-eu01-euwest1.api.smartthings.com

otherwise it will not work.

[1] see: https://community.smartthings.com/t/hub-not-showing-up-on-graph-api-smartthings-com/74022
[2] see: https://community.smartthings.com/t/faq-how-to-find-out-what-shard-cloud-slice-ide-url-your-account-location-is-on/53923


And if you have problems with displaying characters, you should add this to line #52

	print "<html><head><meta charset=\"utf-8\"/>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment