How to get Cloud Endpoint

Progressā€¦ pulling the things from the HousePanel app in groovy on Hubitat and mixing successfully with those from SmartThingsā€¦ I am writing the panel to read from both systems so they can co-exist into the same HousePanel instance since most people will migrate gradually.

1 Like

This worked (sort of). To get it to work I had to use the Authorization Bearer call before invoking curl in PHP. Here is what the code looks like:

$host = HUBITAT_HOST . "/apps/api/" . HUBITAT_ID . "/" . $path;
$headertype = array("Authorization: Bearer " . HUBITAT_ACCESS_TOKEN);
$nvpreq = "access_token=" . HUBITAT_ACCESS_TOKEN;
$response = curl_call($host, $headertype, $nvpreq, "POST");

where curl_call is a simple function call to invoke curl in PHP. I grabbed the id and access_token from a log and stored it in a PHP constant. In the future I will use a post function to send it over to the code. I think this is how AT works. That way a user will only need to enter the URL of their HP installation.

1 Like

Just a quick tip on how to dynamically build a local endpoint in an App. Just replace the <YOUR_PATH_HERE> with your appā€™s path. The only change I made to what was posted earlier in this thread is a method to retrieve the hubā€™s local IP address on the fly.

	  	def localEndPoint = "http://${location.hubs[0].getDataValue("localIP")}/apps/api/${app.id}/<YOUR_PATH_HERE>?access_token=${state.accessToken}"

Hopefully this helps someone else. The more local-to-local integrations we can create, the better!

2 Likes

Thanks Dan ... very useful for pushing the hub settings over to my PHP script to eliminate need for user setup.

Ken,

Which Hubitat firmware version are you currently running? I believe version .678 has some additional helper functions for generating OAUTH endpoint strings which make this even easier. I just learned of them from Josh (@josh). My hub did update successfully to .678 before it was pulled due to a few users having issues with the update process.

If you have .678, you may want to give these a try:

Cloud

  • getApiServerUrl()
  • getFullApiServerUrl()
  • apiServerUrl(String url)
  • fullApiServerUrl(String url)

Local

  • getLocalApiServerUrl()
  • getFullLocalApiServerUrl()
  • localApiServerUrl(String url)
  • fullLocalApiServerUrl(String url)

So, my previous post would change to simply be:

    def localEndPoint = fullLocalApiServerUrl("<YOUR_PATH_HERE>") + "?access_token=${state.accessToken}"
4 Likes

Iā€™m not sure ā€”- not home right now but will check. Iā€™m calling Hubitat from PHP so I have to use authorization bearer header calls for the access token. Rest should work fine. Thanks for the tip.

Sorry about resurrecting such an old thread, but I am looking for how to properly get the access_token and endpoiint using OAUTH2 flow. I have been using the methods described here and I now understand that OAUTH2 flow is fully implemented and returns this information if one has a client_id and a client_secret just like it works on SmartThings. Do I have this right? If so can someone advise if the calls are the same with cloud.hubitat as the url to use to make the oauth calls?

I still don't understand how to get the access token for an app. Do you have to do that within the app? I'm very confused.

Which app? If it's a custom app, then yes, you would do it within the app itself.

Something like:

if(!state.accessToken){	
    createAccessToken() //be sure to enable OAuth in the app settings or this call will fail
}
def localUri = getFullLocalApiServerUrl("optional/path") + "/?access_token=${state.accessToken}"
1 Like

Okay...but how do I get that token to use? That's the part that I'm missing.

Depends on how you want to use it... display it in the app configuration, write it to the logs and copy it from there, post it to another local server, etc.

Here's an example rendering it out into an app preference (configuration) as a paragraph:

preferences(){
    page(name: "setupScreen")
}
def setupScreen(){
   if(!state.accessToken){	
       createAccessToken() //be sure to enable OAuth in the app settings or this call will fail
   }
   def localUri = getFullLocalApiServerUrl("optional/path") + "/?access_token=${state.accessToken}"
   return dynamicPage(name: "setupScreen", uninstall: true, install: true){
      section(){ 
         paragraph("Use the following URI to access the endpoint:<br />Local: <a href='${localUri}'>Local URL</a>")
      }
   }
}
2 Likes

Thank you!

@chuck.schwer I am attempting to port the life360 st app over to hubitat. Was having trouble with oauth and finally see what the issue is I think.

When we authorize against life360 and it hits the redirect url in the format above it adds its token the url with ?access_token=token.

Hubitat seems to take this second token and rejects it with an error page. If I copy the correct token in place the app then authorizes.

So my question is there any other format I can use for the redirect url that will get around this.

I cannot edit the life360 side of things at all.

Currently on my phone. If you need more information or if the question is not clear let me know!!!

Not that i know of. It appears that ST implemented another way of doing it, so I can add that on to our feature request list.

1 Like

Mm too bad. Thanks as always @chuck.schwer

from what I can see the only real diff is how the access token is passed.

smartthings.

serverUrl + "/api/token/${state.accessToken}/smartapps/installations/${app.id}/receiveToken

hubitat

"${getApiServerUrl()}/${hubUID}/apps/${app.id}/receiveToken?access_token=${state.accessToken}"

Just curious - why are you porting the ST Life360 to HE when there is already native Life360 support built into Hubitat?

See this thread

So after much banging my head against the wall I went the password auth route and all is well.

From what I read this is a how parameters are read from the url. When a key is passed more than once the second key:value pairs overwrite the first. So Hubitat doesnt accept the call. I then cannot save the access token trying to be passed in.

So just a quick question to @chuck.schwer can an alternate key name easily be added so we can pass the access token with a different key?

I am thinking this is a very edge case. However it drove me nuts for a few days.