What am I missing to get my Shelly script to work?
I have created a API key with the “Devices” and “Flows” permissions using this guide, https://support.homey.app/hc/en-us/articles/8178797067292-Getting-started-with-API-Keys
I have gotten the device ID of the device I want to fetch the status of (Homey Developer Tools).
When I run my script using the API key I generated I get a 401, HomeyErrorUnauthorized
If I make a request towards http://MY_HOMEY_IP I get an object with the homeyId and homeyVersion back, so the IP is correct.
But when I make a request towards http://MY_HOMEY_IP/api/manager/devices/device/DEVICE_ID/capability/CAPABILITY_ID and sending in the ‘Authorization’: 'Bearer ’ + HOMEY_API_KEY I get the auth error.
const HOMEY = {
apiKey: "xxx-xxx-xxx",
ipAddress: "X.X.X.X"
}
const DEVICE_ID = "xxx-xxx-xxx"
const BASE_URL = 'http://' + HOMEY.ipAddress + '/api/manager/devices/device/';
function getHomeyData() {
let url = 'http://' + HOMEY.ipAddress;
Shelly.call(
"http.get",
{
url: url
},
function (response, error_code, error_message) {
if (response.code === 200 && response.body) {
let homeyData = JSON.parse(response.body);
print('Homey Data:', JSON.stringify(homeyData))
} else {
print("Error Code:", error_code);
print("Message:", error_message);
print("Body:", JSON.parse(response.body));
if (response.code === 401) {
print('Unauthorized: Please check your API token and permissions.');
}
}
}
);
}
function getLockState(deviceId, capability) {
let url = BASE_URL + deviceId + '/capability/' + capability
print('URL:', url);
Shelly.call(
"http.get",
{
url: url,
headers: {
'Authorization': 'Bearer ' + HOMEY.apiKey
}
},
function (response, error_code, error_message) {
if (response.code === 200 && response.body) {
let capabilityValue = JSON.parse(response.body);
print('Capability Value:', capabilityValue);
} else {
print("Error Code:", error_code);
print("Message:", error_message);
print("Body:", JSON.parse(response.body));
if (response.code === 401) {
print('Unauthorized: Please check your API token and permissions.');
}
}
}
);
}
getHomeyData();
getLockState(DEVICE_ID, 'locked');