10.02.2022, 16:40
(This post was last modified: 10.02.2022, 16:53 by Erwin van der Zwart.)
Code:
require('json')
if err == 201 then -- Get Bearer Authorization
--log(res) -- Here is the Bearer Authorization in how do I get the access_token out? And how do I do a json GET with that token?
data = json.pdecode(res)
tokentype = data.token_type
accesstoken = data.access_token
expiresin = data.expires_in
id = data.id
log(tokentype)
log(accesstoken)
log(expiresin)
log(id)
end
Code:
-- Load required packages
require('ltn12')
require('socket.http')
require("json")
-- START OF SETTINGS ************************************************************
-- Set credentials
username = "xxxxx"
password = "xxxxx"
-- END OF SETTINGS **************************************************************
-- START OF FUNCTIONS ***********************************************************
function GetAuthData()
local request_url = 'https://xxxx.com/api/v3/xxx/auth'
local response_body = {}
local request_body = json.encode(
{
email = username,
password = password
}
)
local body, code, hdrs, stat = socket.http.request{
url = request_url;
method = "POST";
headers =
{
["Content-Type"] = "application/json";
["Content-Length"] = #request_body;
};
source = ltn12.source.string(request_body);
sink = ltn12.sink.table(response_body);
}
if code == 200 then
ret = table.concat(response_body)
ret = json.pdecode(ret)
ret.expires_at = os.time() + ret.expires_in
return ret
end
end
function APIRequest(method, api_key, request, requestdata)
local request_url = 'https://xxxx.xxxx.com/api/v3' .. request
local response_body = {}
local request_body = requestdata
local body, code, hdrs, stat = socket.http.request{
url = request_url;
method = method;
headers =
{
["Content-Type"] = "application/json";
['Content-Length'] = #request_body,
["Api_key"] = api_key;
};
source = ltn12.source.string(request_body);
sink = ltn12.sink.table(response_body);
}
if code == 200 then
ret = table.concat(response_body)
ret = json.pdecode(ret)
return ret
end
end
-- END OF FUNCTIONS ***********************************************************
-- START OF AUTHENTICATION ****************************************************
if not authdata then
authdata = GetAuthData()
else
if authdata.expires_at - 60 < os.time() then
-- renew authdata
authdata = GetAuthData()
end
end
-- END OF AUTENTHICATION ******************************************************
-- START OF DATA REQUESTS *****************************************************
request_data = json.encode({})
resp = APIRequest("GET", authdata.api_key, "/location", request_data)
log(resp)
-- END OF DATA REQUESTS *******************************************************
-- START OF UPDATE REQUESTS ***************************************************
request_data = json.encode({
id = 12,
customproperties = {
{
label = "mylabel",
value = "myvalue"
}
}
}
)
resp = APIRequest("PUT", authdata.api_key, "/probe", request_data)
log(resp)
request_data = json.encode({
id = 12,
name = "new name"
}
)
resp = APIRequest("PUT", authdata.api_key, "/location", request_data)
log(resp)
-- END OF UPDATE REQUESTS *****************************************************