Ron
(Ron)
May 18, 2015, 3:57am
1
I am working on a REST Endpoint trying to follow these instructions from @Ben
SmartApp endpoints are a powerful way to securely control your SmartThings with familiar REST calls GET,POST,PUT or DELETE. You have the freedom to define your URL schema and can pass query values through the URL path or through JSON.
This tutorial will guide you through the process of setting up this App Endpoint Example . After you authorize the app to see specific devices, it will generate a web page of buttons with URLs to toggle your chosen lights and locks.
You can download the full code for the SmartApp Endpoint Example and PHP code to interact with the endpoint. Additional OAuth documentation can be found here.
Step 1: Enable OAuth in the SmartApp
To begin, add a New SmartApp in the IDE . Make sure to click the button to âEnable Oauth in Smart App.â Note the âOAuth Client IâŚ
Everything works fine util step 3
Step 1: I redirect to get the code. ST login and device selection is displayed and I select the devices I want access to.
This works.
Step 2: I am returned json with the access_token
I get the access token from the json and forward to myself
this works
Step 3: I reach my routine with the access_token populated and make the call to get the data. But I am always returned only
Not sure how to debug this at this point since ST is always returning empty results.
The call I make in step 3 is
https://graph.api.smartthings.com/api/smartapps/endpoints/$client?access_token= âŚ
where ⌠is my access token. I write the token I get from step 2 and it matches what is in this request.
I donât know php that well so I am guessing at Benâs code.
I am writing my code in Spring Boot.
I notice Ben makes the calls differently for the access_token call which returns json and the call which returns the data.
The first uses curl
$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);
The second uses file which I donât really understand
$url = âhttps://graph.api.smartthings.com/api/smartapps/endpoints/$client?access_token= â.$_REQUEST[âaccess_tokenâ];
$json = implode(ââ, file($url));
I think the difference between these may be my issue.
I am using the same technique for both steps and wondering if this is what I am doing wrong.
Why is the different technique used for the second step.
As a test I just forward in step 3 to the url
https://graph.api.smartthings.com/api/smartapps/endpoints/$client?access_token= âŚ
and I get the blank displayed in the browser.
1 Like
Ron
(Ron)
May 19, 2015, 3:30am
2
I figured out my problem
https://graph.api.smartthings.com/api/smartapps/endpoints/$client?access_token= âŚ
I put literal $clientâŚneeded that to me my client ID.
But now I am dealing with the fact that the first call which returns a list of endpoints doesnât return valid json. JSONObject thows json exception trying to parse. I wish I could read php code better but I think I have it nowâŚ
For some reason this returns a crazy key/value string which I am trying to figure out how to parse easily (or correctly)
The url call to get endpoints returns the following string including the open and close branckets
[{âoauthClientâ:{âclientIdâ:âxxxxxxx-xxxxx-xxxxx-xxxx-xxxxxxxxxâ,âauthorizedGrantTypesâ:âauthorization_codeâ},âurlâ:â/api/smartapps/installations/xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxxxâ}]
This does not parse as json (why didnât they just return json ?
The php code loops first on this as an array of $key $value endpoints. I have only one so my sample is an array of one key/value.
Buy I am not sure how to parse this in Java code. Since it is a mix of json and Map output. It seems messy to me.
Parsing it by had I think it is the following.
endpoints[0]= everything inside the pair perhaps split on , ?
endpoints[0]=
{âoauthClientâ:{âclientIdâ:âxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxâ,âauthorizedGrantTypesâ:âauthorization_codeâ},âurlâ:â/api/smartapps/installations/xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxâ}
I donât understand the code here
foreach ($theEndPoints as $k => $v)
what will be used from the string endpoints[0] for key and what for value ?
then value is accessed as $v[âurlâ] so this is a HashMap itself ?
Does anyone know how to parse this string in Java ? I am confused
tgauchat
(ActionTiles.com co-founder Terry @ActionTiles; GitHub: @cosmicpuppy)
May 19, 2015, 3:33am
3
Ron:
[{âoauthClientâ:{âclientIdâ:âxxxxxxx-xxxxx-xxxxx-xxxx-xxxxxxxxxâ,âauthorizedGrantTypesâ:âauthorization_codeâ},âurlâ:â/api/smartapps/installations/xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxxxâ}]
I ran this through âJSON Lint â and it parses just fineâŚ
1 Like
Ron
(Ron)
May 19, 2015, 3:35am
4
HmmâŚwhy is JSON throwing a parse JSONExceptionâŚinterestingâŚ
JSON lintâŚcoolâŚ
tgauchat
(ActionTiles.com co-founder Terry @ActionTiles; GitHub: @cosmicpuppy)
May 19, 2015, 3:38am
5
Yeah⌠it really helps to have a suite of debugging tools; I just googled to find that one
Thereâs a web based Groovy tester too, and so on⌠(Though Iâm not sure you can load libraries into it).
Ron
(Ron)
May 19, 2015, 3:42am
6
Yea I love using
xpath tester http://www.xpathtester.com/xpath
and jsfiddle is my favorite
https://jsfiddle.net/
should have thought of a json parsing tool LOL
Ron
(Ron)
May 19, 2015, 3:44am
7
OK JSONObject throws exception with the following error
Error:A JSONObject text must begin with â{â at character 1
Why does jsonlint accept the [ ]'s ?
If I strip of the [ ] then JSONObject throws this error.
Error:Expected a â,â or â}â at character 184
tgauchat
(ActionTiles.com co-founder Terry @ActionTiles; GitHub: @cosmicpuppy)
May 19, 2015, 3:49am
8
Hmmm⌠Perhaps JSON lint accepts partial objects? It does seem that the âentireâ object should be surrounded by {}
, and only array / maps are []
.
There must be a detailed JSON syntax spec, of course, but found this:
Letâs have a quick look on JSON basic syntax. JSON syntax is basically considered as subset of JavaScript syntax, it includes the following:
Data is represented in name/value pairs
Curly braces hold objects and each name is followed by â:â(colon), the name/value pairs are separated by , (comma).
Square brackets hold arrays and values are separated by ,(comma).
Hereâs the syntax diagrams: http://json.org/
Ron
(Ron)
May 19, 2015, 3:54am
9
Figured it out. For this json I need to create a JSONArray not a JSONObject.
java, android
Learning a lot here
1 Like