[SOLVED] Struggling to set up Oauth / token bearer in new binding

Trying to establish a new binding towards a bridge requiring Oauth access token. The developers of the bridge have opened their API, and customers are able to retrieve the token by logging in to the customer page, thus the plan is to use the access token as manual input as part of bridge creation.

So, have been trying the org.eclipse.smarthome.io.net.http.HttpUtil route, using Properties httpHeader for input headers, but struggle to access the bridge.

Following request is working using Postman:

curl -X POST \
-H 'cache-control: no-cache' \
-H 'content-type: application/json' \
-H 'Authorization: Bearer 

Trying to set up the same for my binding (using Eclipse IDE):

Properties httpHeader = new Properties();
httpHeader.put("cache-control", "no-cache");
httpHeader.put("content-Type", "application/json");
httpHeader.setProperty("Authorization: Bearer ", MY_PERSONAL_TOKEN);

However, when running:

HttpUtil.executeUrl("POST", BRIDGE_API_ADDRESS, httpHeader, inputStream, JSON_CONTENT_TYPE, REQUEST_TIMEOUT);

I get the following response from the bridge (set up with GraphQL):

{"errors":[{"message":"No valid access token in request","locations":[{"line":1,"column":2}],"path":["viewer"],"extensions":{"code":"INTERNAL_SERVER_ERROR"}}],"data":null}

What happens if you use put here instead of setProperty?

It must be something using “Authorization” and "Bearer " + MY_PERSONAL_TOKEN
BTW is this OAuth2? Because then it’s better to use the OAuth2 support from openHAB core.

1 Like

Did you try:

httpHeader.setProperty("Authorization:", Bearer MY_PERSONAL_TOKEN);

I had to do that Bearer thing too and for my api it works this way.


Generates the same error.

Still, the same error.

It is described as OAuth, however, digging deeper, it seems to be OAuth 2.0, so I will have a look at OAuth2 support from openHAB core

Do you need to put your token in quotes too?

The token is retrieved from a config class within the binding (String value), thus my attempt to set up the method: httpHeader.setProperty("Authoriation: Bearer", getToken as String value).

Have also tried different approaches, defining token manually within method, both with and without quotes, but it always ends up with the same error.

From using the Properties httpHeader approach, I see that the POST is generated with String =value. Not sure if this could be causing the error.

Actually: I was concluding a bit too fast.

Made some changes, and by using StringBuilder("Bearer ") and .append(MY_TOKEN) I managed to get desired response from the bridge using httpHeader.put as suggested.


Glad that it is working now.

What i also found very useful for such things is trying the api out in postman.
I have several setups that I can test within that app.

Thanks, appreciate your tips!

Have been looking into your Pixometer binding to get additional tips, including using JsonObject / JsonParser thus trying to extract certain elements from the response. My current issues are:

  1. My server use GraphQL, and my first query (implementing token bearer from discussion above) gives me the HomeID to be included for further queries. So, using HttpUtil.executeURL gives me the following String response from the bridge:


When using JsonObject jsonResponse = new JsonParser.parse(String_response) and further responseJson.get("id").toString() I do not get anything extracted. Currently have to use String.substring() as a workaround to extract HOME_ID.

  1. I am also implementing the HomeID query / method call within bridge initialize. Further use if/else within query/method to check if query response contains “id”, and set ThingStatus.ONLINE if true and ThingStatusOFFLINE if false. I know the “id” part is in place, but regardless, the OH demo gives me a java.lang.NullPointerException:

    ERROR o.e.s.c.i.c.AbstractInvocationHandler:100 - An error occurred while calling method 'ThingHandler.initialize()' on 'org.openhab.binding.tibber.internal.handler.TibberHandler@xxxxxxx': null

Don’t look toooo much, its still in review. :smiley:

  1. Does get lookup the whole tree for a match?
    I would do that with responseJson.data.viewer.homes[0].id

  2. Can’t help much on that. Maybe someone else can say something about it.