How can I know what is the location for my issued access token?


(Frank) #1

Hi all,

I have configured several locations for my SmartThings account and I want to be able to obtain access tokens attached to any of them. I follow the authorization process and receive my access token correctly, but when I use it to query the endpoint “https://graph.api.smartthings.com/api/smartapps/endpoints” I receive an array with all the smartapps instead just the one I am interested in.

Here is the question: how can I know what item on that array is the one that the access token was issued for?

Any idea or suggestion on this would be great.


(ActionTiles.com co-founder Terry @ActionTiles; GitHub: @cosmicpuppy) #2

You should receive a URI along with the token, no?


#3

You are talking about the SmartThings API which is still “in-development”, right? If so, it’s right in the docs.
http://smartthings.developer.samsung.com/develop/api-ref/st-api.html#tag/devices
You have to list your location ID with your request.


(Frank) #4

Hi Ryan, thank you for your reply.

I am using the “old” API. I follow the process depicted here:
http://docs.smartthings.com/en/latest/smartapp-web-services-developers-guide/authorization.html

I receive the authorization code and the access token correctly. Then I use it to “Get Smartapp endpoints” and I receive an array with several items, similar to the following:

[
{
“oauthClient”: {
“clientId”: CLIENTIDX
},
“location”: {
“id”: LOCATION1_ID,
“name”: LOCATION1
},
“uri”: URI FOR INSTALLATIONW,
“base_url”: BASE URL,
“url”: “/api/smartapps/installations/INSTALLATIONW”
},
{
“oauthClient”: {
“clientId”: CLIENTIDX
},
“location”: {
“id”: LOCATION2_ID,
“name”: LOCATION2
},
“uri”: URI FOR INSTALLATIONZ,
“base_url”: BASE URL,
“url”: “/api/smartapps/installations/INSTALLATIONZ”
},
{
“oauthClient”: {
“clientId”: CLIENTIDX
},
“location”: {
“id”: LOCATION3_ID,
“name”: LOCATION3
},
“uri”: URI FOR INSTALLATIONY,
“base_url”: BASE URL,
“url”: “/api/smartapps/installations/INSTALLATIONY”
},
{
“oauthClient”: {
“clientId”: CLIENTIDX
},
“location”: {
“id”: LOCATION3_ID,
“name”: LOCATION3
},
“uri”: URI FOR INSTALLATIONX,
“base_url”: BASE URL,
“url”: “/api/smartapps/installations/INSTALLATIONX”
}
]

When I use the same token to reach the endpoints on my smartapp, the smartapp uses one of the locations on the array, but not necessarily the one that I selected when I issued the access token.

I want my smartapp to operate with the location that the access token was issued for. I think that I could solve this issue if I was able to identify the last smartapp installation on that array, which should be the one related to the last access token. I tested to see if new installations where added at the position 0 of the array, and it seems to be true when there is no other installation for the same location, but not in other case.


(Frank) #5

Hi Terry, thank you for your reply.

I hope my answer to Ryan clarifies what is the issue.


#6

What smartapp are you trying to make a call to?


(Frank) #7

The thing is that all those are installations of the same smartapp. All of them point to the smartapp that I want to make the call to, but when I trigger an endpoint of the smartapp from my server, the location it uses is not the one that I selected when I issued the access token.


#8

The token has access to all your locations. You have to specify which one.


(Frank) #9

Let me explain a little bit more the scenario.

A user follows the authorization process for SmarThings from my app. During this process the user is prompted with a screen to choose a location. Something similar to this:

13%20PM

Then obtains an access token and therefore is able to access the smartapps and their endpoints.

Now, from the perspective of the server, how can I know what location the user chose?


#10

You don’t need to. Only ST does. It will then link the devices that the user authorized.


(ActionTiles.com co-founder Terry @ActionTiles; GitHub: @cosmicpuppy) #11

Are you talking new API or the original?

You can get the current instance Location information from the location.* object.


(Frank) #12

I am triggering an action from my server, calling one of the endpoints of the smartapp.
I need to know what location the user selected in order to call the smartapp on the correct installation uri. Only then the action would be triggered on the location that the user expects.

What I currently do from my server is calling https://graph.api.smartthings.com/api/smartapps/endpoints and taking the uri of the first element in the array. I assumed that the last installation would be in the first position of the array, but I’ve observed that this is only true if there is no previous installation of the app for the specified location. If a previous one exists and is not in the first position of the array, I end up triggering the action from my server for a wrong location.