Variable token required in the Header

  • Platform information:
    • Hardware: _Intel 7i/ 32GB /500 GB
    • OS: _Windows 11
    • openHAB version: 4.0.1

Hello All,

I would just like to check my understanding with guys to ensure I implement this correctly. I have spent a lot of time on the documentation and on the community and it seems that this step can only be done through command line execution.

I am using http binding to make API calls to a service offered by my backup power vendor. I get details like gird power consumption, battery state of charge etc from this API by making several calls to it.
(Below is code of the HTTP Thing for reference).

In order to get the data, I need to provide a token (which is of course specified in the header.) The token I get by making a specific call providing my username + password. Currently I make this call manually using reqbin or postman and copy and paste the token into the header of the HTTP Thing. It runs for a few days before it needs to be refreshed. This of course needs to be automated which means the token in the header of the HTTP Thing needs to be a variable.

Is my understanding correct that this can only be done using an executeCommandLine?
Is there no way that I can setup another HTTP Thing or a new channel on the current HTTP Thing that can first retrieve the token to be used by the subsequent calls?

Thanks in advance for any insights.

The code for the HTTP after I have manually put the token in the header:
UID: http:url:binHTTPSunSynk
label: HTTP URL Sunsynk
thingTypeUID: http:url
configuration:
authMode: DIGEST
headers:
- Authorization = Bearer zYbQoYNKe3Vu3_nx8C8M3t-xxxx
ignoreSSLErrors: false
baseURL: https://api.sunsynk.net/api/v1
delay: 0
stateMethod: GET
refresh: 10
commandMethod: GET
contentType: application/json
timeout: 3000
bufferSize: 2048
channels:

  • id: chnInverterGridData
    channelTypeUID: http:string
    label: Inverter Grid Data
    description: “”
    configuration:
    mode: READONLY
    stateExtension: /inverter/grid/230218xxxx/realtime
  • id: chnInverterBatteryData
    channelTypeUID: http:string
    label: Sunsynk Battery
    description: “”
    configuration:
    mode: READWRITE
    stateExtension: /inverter/battery/230218xxxx/realtime?lan=en
  • id: chlPlant
    channelTypeUID: http:string
    label: "Plant "
    description: “”
    configuration:
    mode: READONLY
    stateExtension: /plant/energy/25xxxx/flow

This should be theoretically possible entirely within OH, but it requires one little trick. You would first set up one http thing to get the barer token into an item (every 2 days or whatever your preferred interval is). Then you would need to use one of the scripting languages that can access the Thing registry (basically not dsl) and use a rule that triggers when that item updates to modify the other http Thing configuration with the new bearer token stored in the item state.

Thank you Justin,

I have tried this using the API Explorer in Developer Tools, and this does work - I managed to update the HTTP Thing configuration using PUT methods.

I just now have to figure out how to do this in Javascript - form the documentation it is not yet obvious to me but I’ll keep at it. If I don’t come right I’ll post my closest attempt here again for some guidance.

Thanks for the effort in responding - much appreciated.