Send POST Request for Access Token

Tags: #<Tag:0x00007f172d161610>

Hi there,

is there someone, who can help me out with a hint. I am trying to send a HTTP POST Request to an API from within openHAB. The result should be an Access-Token, that I need to send another POST to control a device.

Within Postmaster I am able to send the Request with:
{{url}}/api/authorize/access_token
and get a JSON-Response.

Postmaster translates it into:

curl --location --request POST 'http://192.168.20.21/api/authorize/access_token' \

--header 'Cookie: PHPSESSID=7gpadbudr9fk0ft16s3mlrfu25' \

--form 'client_id=NbgigQlMFZQ4eMqTpI0w0WPmZ6wW9z1IcLThTVlM' \

--form 'client_secret=6et0YuL28BSnd3Xyin820CejFqn2wRsVTP7QnQtw6mqykKvrMRHuFaWKMQxR2EbS0PNWHmEFKd9hmMErYN23Q5V06691RjWb2mq8mD0xnaU5bS6VwbdWjN4imSkBQKWDdmiuPGS214DhqAXkrjiXXkGKcRVLiTDXyGt93TjTqU3mN3Da485n92u60rR7LJnMf1BOex2Ke4S6Euu0TyIRyf0aGw4W72khAPsKookzEfYXijoXBbW57XvH3HnC2L' \

--form 'grant_type=client_credentials'

The result is:

{

    "access_token": "saCWin2vMjithNaRL9VUZF6iO3kfkHpsxJiYfQdP",

    "token_type": "Bearer",

    "expires_in": 3600

}

I have frankly no idea where to go from here. Is it possible, do send a header with the HTTP-Binding? Can I do this with a sendHttpPostRequest?

Help would be very much appreciated!

Regards,

Nikolaus\

I assume this is OH3.
You need a key from username/password authentication before requesting a token.

I believe the latest snapshot has basic Auth enabled.

Right now I’m on 2.5.9.

Is it necessary to use OH3? Maybe its time to switch…

I have not used the REST APi on OH2. I did not remember a token being needed.

Sorry, maybe I misguided you there.

I want to make a POST request to access the API of another application running on the same docker server. This application needs the access-token in the Header of a POST request.

1 Like

You can continue to use curl for the next step as well.
I would expect that the request needs to look similar as in this example: https://developer.paypal.com/docs/api/get-an-access-token-curl/

1 Like

That solution needs the Exec Binding, right?

My openHAB-Setup runs from within a docker container. I’ve read somewhere, that it won’t work from inside a container.

See this discussion. Yes, it is possible. For 2.5.x, you have to download the jar file. It will be included in OH 3.

Thanks for the hint.

So… I get that right: With the alternative HTTP Binding I can send an sendHttpPostRequest with a header?

I still don’t get how that works exactly. Can I use an Item with a {http} channel for that? How do I add the header to that?

I’m sorry if that is a stupid question, I don’t have much experience with the HTTP-Binding.

Thanks for your help!

This is an action which you can find described here: https://www.openhab.org/docs/configuration/actions.html#http-actions

You would use it in a rule. No channel involved. The post I cited has an example of using it in a rule. You collect and parse the output from the command as the return result.

Note that it is probably also possible to use the new HTTP v2 binding. I have not tried that. What I’m referring to above is the action.

1 Like

Thank you for your help!

I have a working solution now, that depends on some shell-scripts running from within the scripts folder. I get the key from the getAccessToken.sh:

    #!/bin/bash
    curl --location --request POST 'http://myxiboserverip/api/authorize/access_token' \
    --header '' \
    --form 'client_id= XXXX ' \
    --form 'client_secret=XXXX' \
    --form 'grant_type=client_credentials' > /openhab/conf/scripts/outfile

I then cut the token from the outfile with a JSONPATH-Transformation.

Might not be an elegant solution, but it works perfectly. :wink: I use it to control a Digital Signage Display. I can now retire, unretire or push screens to my Digital Signage System via openHAB.