This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

External eletric pricing on API
#5
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
Here is an sample of integration i wrote with authentication token renewal that uses the expire stamp of the token, with this sample you should be able to create what you want to do
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 *****************************************************
Reply


Messages In This Thread
External eletric pricing on API - by Tue - 09.02.2022, 09:20
RE: External eletric pricing on API - by Tue - 09.02.2022, 12:30
RE: External eletric pricing on API - by Tue - 10.02.2022, 16:29
RE: External eletric pricing on API - by Erwin van der Zwart - 10.02.2022, 16:40
RE: External eletric pricing on API - by Tue - 11.02.2022, 15:01

Forum Jump: