Hi there,
I’m trying to send command to my OH3 Items via an external web page with some Javascript code.
So far I’m successfully getting item status but I’m struggling with command actions (ON & OFF).
The script I’m trying to use is the following:
function command() {
const xhr = new XMLHttpRequest();
xhr.open("POST", "https://IP/rest/items/someItem");
xhr.setRequestHeader("Content-Type", "text/plain");
xhr.setRequestHeader("accept", "*/*");
xhr.setRequestHeader("Authorization", "Bearer SomeBearer");
xhr.send("ON");
}
What I’ve tried:
- I’ve put some Bearer as I found from my OH3 admin tab, Developer Tools/Api Explorer but I got a 401 error - Authorization response from server
xhr.setRequestHeader("Authorization", "Bearer SomeBearer");
- I thought I need some api token to send with the request. So I created one from the admin tab, activated the Basic Auth from Settings/API protection and tried to send the request this way:
xhr.setRequestHeader("Authorization", "Basic oh.APIs.someToken");
I was not successful…
- Changing the header this way:
xhr.setRequestHeader("Authorization", "Bearer oh.APIs.someToken");
I got a 405 Error - Method not allowed
- Tryed to use alternative headers like below
xhr.setRequestHeader("Authorization", "Basic" + btoa("oh.APIs.someToken));
and
xhr.setRequestHeader("Authorization", "Basic" + btoa("admin" + ":" + "myAdminPassword"));
and
xhr.setRequestHeader("Authorization", "Basic" + btoa("admin:myAdminPassword"));
and
xhr.setRequestHeader("Authorization", "Bearer myToken");
xhr.setRequestHeader("WWW-Authenticate", "Basic");
still no luck…
Do you have any suggestion on how to make this request working?