Rest API Authentication in Rule

  • Platform information:
    • Hardware: Synology RS1221+
    • DSM 7.2.1, Docker 20.10.23
    • Java Runtime Environment: ?
    • openHAB version: 4.1.1
  • Issue of the topic: I would like to write a DSL rule that would run once a day and send a notification of any Things are not online. I have not been able to find a clear (to me) example that is using token authorization. I would appreciate it is someone could point me to an example of let me know what is wrong with the following.

Right now I’m just attempting to extract the items. Once this is working I’ll work on extracting and filtering the Things.

rule TestRule
when Item TestSwitch changed
then logInfo("TestRule", "Beginning")
     val headers = newHashMap("Authorization" -> "Bearer jE88PP......BVkw", 
                              "Accept" -> "application/json",
                              "WWW-Authenticate" -> "Basic")
     var hostURL = "http://192.168.0.5:8080/rest/items?recursive=false"
     var jasonData = sendHttpGetRequest(hostURL, headers, 5000)
     logInfo("TestRule", jasonData)
end
2024-04-16 16:37:57.937 [INFO ] [g.openhab.core.model.script.TestRule] - Beginning
2024-04-16 16:37:57.938 [WARN ] [openhab.core.io.rest.auth.AuthFilter] - Unauthorized API request from 192.168.0.5: Error while processing JWT token
2024-04-16 16:37:57.939 [ERROR] [enhab.core.model.script.actions.HTTP] - Fatal transport error: java.util.concurrent.ExecutionException: org.eclipse.jetty.client.HttpResponseException: HTTP protocol violation: Authentication challenge without WWW-Authenticate header
2024-04-16 16:37:57.940 [INFO ] [g.openhab.core.model.script.TestRule] - null

Thanks in advance,
Jim

Solved – the token value must be proceeded with “oh.tokenname.”

Working rule:

rule TestRule
when Item TestSwitch changed
then logInfo("TestRule", "Beginning")
     val headers = newHashMap("Authorization" -> "Bearer oh.RestApiToken.jE88PP......BVkw", 
                              "Accept" -> "application/json",
                              "WWW-Authenticate" -> "Basic")
     var hostURL = "http://192.168.0.5:8080/rest/things?"
     var jasonData = sendHttpGetRequest(hostURL, headers, 5000)
     var ThingStatus = transform("JSONPATH", "$.[2].statusInfo.status", jsonData)
     logInfo("TestRule", ThingStatus)
     var ThingLabel = transform("JSONPATH", "$.[2].label", jsonData)
     logInfo("TestRule", ThingLabel)
end
1 Like