1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
https =
require(
'ssl.https')
json =
require(
'json')
ltn12 =
require(
'ltn12')
iDebug =
false
cloudUrl =
'https://public-api.cloud.meater.com/v1'
cloudEmail =
'myemail@address.nl'
cloudPwd =
'mypassword'
function getToken()
tbl = {}
local iData = {}
iData[
'email'] =
cloudEmail
iData[
'password'] =
cloudPwd
local iData =
json.encode(
iData)
res,
code =
https.request({
url =
cloudUrl..'/login',
method =
'POST',
headers = {
[
'content-type'] =
'application/json',
[
'content-length'] = #
iData,
},
source =
ltn12.source.string(
iData),
sink =
ltn12.sink.table(
tbl),
})
if iDebug then log(
res,
code)
end
if res and code ==
200 then
resp =
table.concat(
tbl)
resp =
json.pdecode(
resp)
return resp.data.token
end
end
function getData(
iEndpoint)
vToken =
getToken()
if vToken then
res,
code =
https.request({
url =
cloudUrl..iEndpoint,
method =
'GET',
headers = {
[
'authorization'] =
'Bearer ' ..
vToken
}
})
if iDebug then log(
res,
code)
end
if res and code ==
200 then
return res
else
log(
res,
code)
end
end
end
log(
getData(
'/devices'))