OAuth2 using just OH Rules and myopenhab.org

Rich,

I wanted to follow up and say this is awesome. I used your scripts, changed anything “dexcom” to “skydrop” - and ran it. Now I can use HTTP GET and POST commands to control my sprinklers.

I don’t know if it’s BETTER than the wifi app from skydrop… but I’m always happy to get another thing consolidated into OpenHAB.

Now to fiddle with the skydrop API to see if I can make it useful.

Thanks!

I’m glad you found it useful. If you figure out the skydrop API you will have done most of the hard part for a new binding. :wink:

That’s true. I’ll start a new thread for that discussion, don’t want to dump all of that planning into this thread, we’ll keep it for OAUTH2.

Watch for it in a minute or two…

Thanks for your tutorial. Unfortunately I got the following bug, see my logs:
2019-01-16 01:09:12.778 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule ‘Poll Dexcom for data’: The name ‘Dexcom_EGVS_Poll’ cannot be resolved to an item or type; line 125, column 3, length 16

Thanks in advance for a reply.

Best regards,
Jochen

Before spending too much time on this, what is your end goal? I am because of it is specifically to get Dexcom readings, the deejay betwen the readings and the ability to pull them through this API makes it pretty much useless in a home automation context. If that is your goal, you will be better off setting up Nights out which has a much simpler API. I’ve posted a tutorial due integrating it with OH as well.

If you are just trying to get OAurh working, then the specific error means you are missing that Item. in on my phone and about to go to bed so it will be tomorrow veggie I’ll be able to remember what that item is for.

Thanks a lot for your reply rikoshak! Can you send me a link to the tuturial you mentioned - I was not able to find it by googling myself.

My final goal is to create a solution to access a RestAPI which uses OAuth2. Currently I am figuring out if this approach can outperform the idea of developing a full fledged binding, which takes much more time in my opinion. Moreover the binding won’t be useful for third parties, as far as I can guess. What do you think?

Related to the Error-Message: I guess your tutorial has an error in it, because before I can start the call, my log messages tell me, that I need to define the Item somewhere. Can you check, if the tutorial runs on your environment? I guess, that people will try to use it and it is hard and maybe frustrating if the code won’t run. Thanks for offering the tutorial, anyway and maybe thanks in advance for your verification, if this stuff runs in your openhab-instance.

Best regards,
Jochen

The tutorial is missing the Dexcom_EGVS_Poll Item. It is just a Switch.

If you are looking to interact with a service that requires OAuth, the appropriate approach really is a new binding. This tutorial is just intended to be a “plan B” in case a binding is not feasible, or as a way to prototype the interactions with the interface before writing a binding.

Thank you, learned a lot about oauth while pulling my hair out :wink:

I used your example to issue an “open” command to the Nello API. Nello is a gadget that listens for the bell and opens front doors to apartment buildings.
https://www.nello.io/en/
https://nellopublicapi.docs.apiary.io/

Problems were that it only worked with a client credentials grant, the oauth token url needed a trailing slash, and that the API itself wants PUT requests instead of GET. I think the whole authcode callback thing was not needed, and that was the only part that worked straight away :frowning:

But when I forget my frustration and am in an optimistic mood, I might try to leverage this html callback method to get ring events into OH.

I just got the nello device and I am planning to integrate it into Oh, have @AFromD gotten any further?

Sorry, I gave up on that.

I think the reason was that it was impossible to do without my own vserver on the internet. I was hoping I would be able to do it trhough myopenhab or maybe with a LAMP webhosting package I already use. But I wasn’t able to.

Sorry for this question, but in which file do i have to put all the following code below?

Please read some of the later posts. Dexcom throttles the data. Unless you have a use for driving your home automation using blood sugar readings that are 30 minutes or more old the API isn’t going to be useful for you. You would be better off setting up Nightscout and either populating it from CLARITY or using xDrip or Spike on iOS. I’ve shown how to do most of that in NightScout openHAB Integration.

And if you are so new to openHAB that you don’t know where to put Items and Rules files, I recommending stepping back and reviewing some getting started info: How to get started (there is no step-by-step tutorial).

Items go in .items files, code goes in .rules files

1 Like

This whole process used to work perfectly but not anymore.

when working
Google assistant
select openhab
GA redirects to an Oauth2 screen
fill in credentials
all devices from openhab are visible in google assistant

i unlinked my setup (because i could not sync devices anymore)

now when i do the same i get

Google assistant
select openhab
NO OAUTH2 redirect screen anymore
instead is states that Openhab is connected
then after 5 seconds it states "something went wrong, please try again.

i t looks like google is not even trying to reach openhab anymore and does not get the oauth2 screen anymore

who is reponsible for the Openhab service from within google because i think something is messed up(probably after a recent update in june/juli)

could be that everything still works for everybody but not when you unlink and link again

i tried something else.
since openhab is not the problem if figured it must be google

If i login with another google gmail account everything works perfectly

so how can it be that one account fails and another works

can google disable things on an account level?

I don’t think your posts have anything to do with this thread. This is how to implement OAuth2 for cases where there isn’t already support in OH for integrating with a service that requires OAuth2. For Google integration, see https://www.openhab.org/docs/ecosystem/google-assistant/#google-assistant-action or, if you want to use your own hosted version of the openHAB Cloud Server, see https://github.com/openhab/openhab-google-assistant/blob/master/README.md. It requires a lot more than just OAuth2 to integrate openHAB with Google Assistant.

ok clear thanks

@rlkoshak I have been using your http request functions quite a bit for other purposes than OAuth, so many thanks for sharing. Anyhow, maybe as an addition you could also implement exceptions for timeout, host not found etc that you can catch - as this is something that I experienced while using it for other services. This would make it more complete?

Example

val Functions$Function2<String, Functions$Function1<HttpsURLConnection, String>, String> queryGithubAPI = [ endpointURL, readResponse |

try {
    val endpoint = new URL(endpointURL)
    val HttpsURLConnection connection = endpoint.openConnection() as HttpsURLConnection
    connection.requestMethod = "GET"
    connection.setDoOutput(true)
    connection.setConnectTimeout(10000); //set timeout to 10 seconds

    val response = readResponse.apply(connection)

    return response;
    } catch (java.net.SocketTimeoutException e) {
        // logWarn("httpAPI_Github", "Timeout " + e.toString)
        return "ERROR TIMEOUT " + e.toString;
    } catch (java.io.IOException e) {
        // logWarn("httpAPI_Github", "IOException " + e.toString)
        return "ERROR IOException " + e.toString;
}
]

There is a whole lot about the code in the OP that is out of date. It’s not and never was intended to be production code. It’s just an example. So I’m not really inclined to spend any time updating it or making it bullet proof. Even the way the lambdas are defined is out of date.

Furthermore, I’m not sure that Rules DSL is a good approach for doing something like this any longer. Were I to write this again (which might be the case in the not too distant future) I’d use Jython rules which will allow a much more reasonable coding approach.

Hey @rlkoshak
3 years later I also stumbled over this.
As always really good write Up.

As you say it is Out of Date… I think such a approach is of high value actually.
Many want to Connect to another API but have Not the Skill to come Up with a real Binding.
But If there would be an “official” way how you can Talk to a external Rest API via myopenhab this would be a Low Code solution to many?

Interested to learn Others opinions on that.

Cheers