[SOLVED] HTTP Binding (openHAB 2.x) and Authorization Bearer Header

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!

The keyword Bearer in your OH settings is always lower case while you use it upper case in your curl request.
I would suggest to try a lower case bearer in curl in case that does not work you need to use an uppercase Bearer in your OH settings.

Well spotted @Wolfgang_S, and that was indeed the cause of the json string can not be null or empty exception when using the http.cfg method. I did get no output from the curl command with a lower case b.

The remaining ones in my original post were due to copy/pasting the pre-sanitised Bearer key from that, whereas in my .items file it was correct.

Correcting this to an uppercase B, I now still get the 401 Authorization Required error from both methods (using a cached value from http.cfg and defining it entirely in the .items file), so unfortunately that’s still at fault.

:man_facepalming: :man_facepalming: :man_facepalming:

My Bearer token was incorrect in all of my openHAB files. I couldn’t copy/paste from the terminal where I first tried it, so I typed it out again - and added an extra - in the final block!

Removed that, and using the http.cfg cache and .items method, it works perfectly!

:man_facepalming: :man_facepalming: :man_facepalming:

Thank you @Wolfgang_S for pointing me in the direction of the Bearer token - you were partly correct (as per my previous post), but it was mostly because I’d totally messed up the Bearer token and I was indeed not authorized!

So, for anyone stumbling across this later, this works perfectly for getting your Bulb electricity/gas meter values, if you’ve linked it via the SmartThings app (you don’t need a SmartThings hub at all):

http.cfg

smartthingsbulb.url=https://api.smartthings.com/v1/devices/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/components/main/status{Authorization=Bearer xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx}

.items

Number Total_Electricity_Used "Value: [%.3f kWh]" { http="<[smartthingsbulb:60000:JSONPATH($.energyMeter.energy.value)]" }
Number Total_Gas_Used "Value: [%.3f kWh]" { http="<[smartthingsbulb:60000:JSONPATH($.gasMeter.gasMeter.value)]" }
1 Like