I created a test smartapp to drive a PHP application but when trying with the sample PHP code, it gets the access code but then doesn’t get the authorization token. There is no error, but no content in the response to get the authorization token.
This is the top of exampleOauth.php followed by the response:
//client id and client secret
$client = ‘<my_id>’;
$secret = ‘<my_secret>’;
//hardcode the full url to redirect to this file
$url = “http://dadler.dynalias.org/dwacode/smartthings/exampleOauth.php”;
//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);
$curlinfo = curl_getinfo($ch);
print “curl info
”;
print_r($curlinfo);
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);
}
Response:
curl info
Array
(
[url] =>
https://graph.api.smartthings.com/oauth/token?grant_type=authorization_code&client_id=<my_id>&client_secret=<my_secret>&redirect_uri=http://dadler.dynalias.org/dwacode/smartthings/exampleOauth.php&code=rfmr1s&scope=app
[content_type] =>
[http_code] => 0
[header_size] => 0
[request_size] => 0
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.109
[namelookup_time] => 0.047
[connect_time] => 0.078
[pretransfer_time] => 0
[size_upload] => 0
[size_download] => 0
[speed_download] => 0
[speed_upload] => 0
[download_content_length] => -1
[upload_content_length] => -1
[starttransfer_time] => 0
[redirect_time] => 0
[certinfo] => Array
(
)
[redirect_url] =>
)
error requesting access token…