OAuth2 using just OH Rules and myopenhab.org

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