Last active
September 24, 2022 07:17
-
-
Save yuhki50/1b4971febb0bef72ac3bf5255811d59d to your computer and use it in GitHub Desktop.
Request SwitchBot API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const SWITCHBOT_TOKEN = 'your token'; | |
const SWITCHBOT_SECRET = 'your secret'; | |
const devices = [ | |
{ | |
deviceId: "0123456789AB", | |
deviceName: "Contact Sensor", | |
}, | |
]; | |
const responses = UrlFetchApp.fetchAll(devices | |
.map(device => { | |
const params = { | |
headers: { | |
'Content-Type': 'application/json', | |
Authorization: SWITCHBOT_TOKEN, | |
t: new Date().getTime().toString(), | |
nonce: device.deviceId, | |
}, | |
method: 'get', | |
muteHttpExceptions: true, | |
}; | |
params.headers.sign = Utilities.base64Encode(Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_256, [ | |
SWITCHBOT_TOKEN, | |
params.headers.t, | |
params.headers.nonce, | |
].join(''), SWITCHBOT_SECRET)); | |
return UrlFetchApp.getRequest(`https://api.switch-bot.com/v1.1/devices/${device.deviceId}/status`, params); | |
}) | |
.map(request => { | |
delete request.headers['X-Forwarded-For']; | |
return request; | |
}) | |
).map(request => JSON.parse(request.getContentText())); | |
responses.forEach(response => { | |
console.log(JSON.stringify(response, null, 2)); | |
}); | |
const out = devices.map(device => { | |
const res = ; | |
return Object.assign({}, device, { | |
res: responses.find(response => response.body.deviceId == device.deviceId), | |
}); | |
}); | |
console.log(out); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment