Problem implementing endpoint OAuth

I am working on a REST Endpoint trying to follow these instructions from @Ben

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

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 :confused:

I ran this through “JSON Lint” and it parses just fine


1 Like

Hmm
why is JSON throwing a parse JSONException
interesting

JSON lint
cool


Yeah
 it really helps to have a suite of debugging tools; I just googled to find that one :wink:

There’s a web based Groovy tester too, and so on
 (Though I’m not sure you can load libraries into it).

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

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

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/

Figured it out. For this json I need to create a JSONArray not a JSONObject.

Learning a lot here :smile:

1 Like