08.08.2022, 12:07
This works for me using your credentials:
Code:
require('json')
require('ssl.https')
require('ltn12')
-- Set credentials for Ebecoconnect API
userNameOrEmailAddress = '...' -- your email for Ebecoconnect account
password = '...' -- your password for Ebecoconnect account
function GetToken()
local response_body = {}
local request_body = json.encode({
userNameOrEmailAddress = userNameOrEmailAddress,
password = password,
})
local body, code, hdrs, stat = ssl.https.request{
url = 'https://ebecoconnect.com/api/TokenAuth/Authenticate',
method = 'POST',
headers = {
['Content-Type'] = 'application/json',
['Content-Length'] = #request_body,
['Abp.TenantId'] = 1,
},
source = ltn12.source.string(request_body),
sink = ltn12.sink.table(response_body),
}
if code == 200 then
local ret = table.concat(response_body)
ret = json.pdecode(ret)
if type(ret) == 'table' and type(ret.result) == 'table' then
return ret.result.accessToken
end
else
log('request failed', body, code, hdrs, stat)
end
end
API_Token = GetToken()
function RequestFromEbeco(request, params)
local url = 'https://ebecoconnect.com/api/services/app/Devices/' .. request
local response_body = {}
local request_body = ''
local body, code, hdrs, stat = ssl.https.request{
url = url,
method = 'GET',
headers = {
['Content-Type'] = 'application/json',
['Authorization'] = 'Bearer ' .. API_Token,
},
source = ltn12.source.string(request_body),
sink = ltn12.sink.table(response_body),
}
if code == 200 then
local ret = table.concat(response_body)
ret = json.pdecode(ret)
return ret
else
log('request failed', body, code, hdrs, stat)
end
end
if API_Token then
devices = RequestFromEbeco('GetUserDevices')
log(devices)
end