Hi all,
I’m trying to do a simple HTTP request, but I need to pass a Bearer token in the header.
It should return a JSON object, which I will parse to get multiple bits of information from.
I have tried the following:
http.cfg
#smartthingsbulb.url=https://api.smartthings.com/v1/devices/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/components/main/status{Authorization=bearer xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxx-xxxx}
items file
Number Total_Electricity_Used "Value: [%.3f kWh]" { http="<[smartthingsbulb:60000:JSONPATH($.energyMeter.energy.value)]" }
But get the following in the openhab.log
2021-03-07 17:00:18.329 [ERROR] [b.core.service.AbstractActiveService] - Error while executing background thread HTTP Refresh Service
java.lang.IllegalArgumentException: json string can not be null or empty
at com.jayway.jsonpath.internal.Utils.notEmpty(Utils.java:386) ~[?:?]
at com.jayway.jsonpath.internal.ParseContextImpl.parse(ParseContextImpl.java:36) ~[?:?]
at com.jayway.jsonpath.JsonPath.read(JsonPath.java:502) ~[?:?]
at org.openhab.transform.jsonpath.internal.JSonPathTransformationService.transform(JSonPathTransformationService.java:63) ~[?:?]
at org.openhab.core.transform.TransformationHelper$TransformationServiceDelegate.transform(TransformationHelper.java:69) ~[bundleFile:?]
at org.openhab.binding.http.internal.HttpBinding.execute(HttpBinding.java:218) ~[?:?]
at org.openhab.core.binding.AbstractActiveBinding$BindingActiveService.execute(AbstractActiveBinding.java:146) ~[bundleFile:?]
at org.openhab.core.service.AbstractActiveService$RefreshThread.run(AbstractActiveService.java:169) [bundleFile:?]
So, I tried clearing the entry in http.cfg
and put the full URL & header directly to the item:
Number Total_Electricity_Used "Value: [%.3f kWh]" { http="<[https://api.smartthings.com/v1/devices/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/components/main/status{Authorization=bearer xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxx-xxxx}:60000:JSONPATH($.energyMeter.energy.value)]" }
And I now get the following in the log:
2021-03-07 17:03:25.753 [WARN ] [ab.binding.http.internal.HttpBinding] - Transformation 'JSONPATH($.energyMeter.energy.value)' threw an exception. [response=<html>
<head><title>401 Authorization Required</title></head>
<body>
<center><h1>401 Authorization Required</h1></center>
<hr><center>openresty</center>
</body>
</html>
]
org.openhab.core.transform.TransformationException: Invalid path '$.energyMeter.energy.value' in '<html>
<head><title>401 Authorization Required</title></head>
<body>
<center><h1>401 Authorization Required</h1></center>
<hr><center>openresty</center>
</body>
</html>
'
at org.openhab.core.transform.TransformationHelper$TransformationServiceDelegate.transform(TransformationHelper.java:71) ~[bundleFile:?]
at org.openhab.binding.http.internal.HttpBinding.execute(HttpBinding.java:218) [bundleFile:?]
at org.openhab.core.binding.AbstractActiveBinding$BindingActiveService.execute(AbstractActiveBinding.java:146) [bundleFile:?]
at org.openhab.core.service.AbstractActiveService$RefreshThread.run(AbstractActiveService.java:169) [bundleFile:?]
But, if I do the following from the command-line on my openHAB box:
curl -s -H "Authorization: Bearer xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxx-xxxx" "https://api.smartthings.com/v1/devices/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/components/main/status"
This is the response:
{"energyMeter":{"energy":{"value":19958.994,"unit":"kWh","timestamp":"2021-03-07T17:02:27.855Z"}},"healthCheck":{"checkInterval":{"value":600,"unit":"s","data":{"deviceScheme":"TRACKED"},"timestamp":"2020-08-05T11:31:34.283Z"},"healthStatus":{"value":null,"data":{},"timestamp":"2021-03-07T10:57:36.614Z"},"DeviceWatch-Enroll":{"value":null,"timestamp":"2021-03-07T10:57:16.958Z"},"DeviceWatch-DeviceStatus":{"value":null,"data":{},"timestamp":"2021-03-07T10:57:13.953Z"}},"gasMeter":{"gasMeterPrecision":{"value":null,"timestamp":"2020-08-05T11:31:38.758Z"},"gasMeterCalorific":{"value":null,"timestamp":"2020-08-05T11:31:38.758Z"},"gasMeterTime":{"value":null,"timestamp":"2020-08-05T11:31:38.758Z"},"gasMeterVolume":{"value":null,"unit":"m^3","timestamp":"2020-08-05T11:31:38.758Z"},"gasMeterConversion":{"value":null,"timestamp":"2020-08-05T11:31:38.758Z"},"gasMeter":{"value":86768.013,"unit":"kWh","timestamp":"2021-03-07T16:59:32.647Z"}}}
So - the URL and Bearer token are valid - but I’m obviously not able to get the right combination in openHAB.
Any help will be greatly appreciated!