Logic Machine Forum
API ttlock - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: API ttlock (/showthread.php?tid=5235)



API ttlock - Jose - 07.02.2024

Hello everyone

I'm trying to integrate ttlock locks, but I get a little lost with the POST, GET issue
I found a similar script from Husqvarna that I tried to adapt, but I get the nil message.
https://forum.logicmachine.net/showthread.php?tid=4824

Code:
http = require('socket.http')
ltn12 = require('ltn12')
local authEndpoint = 'https://euapi.ttlock.com/oauth2/token'
local clientId = 'd4e938ac8bedxxxxxxxxxxx'
local clientSecret = 'f646xxxxxxxxxxxxxxxxxxxc19'
local username = 'xxxxxx'
local password = 'xxxxxx'
local AccessToken
function getAccessToken()
    local requestBody = 'grant_type=client_credentials'
        .. '&client_id=' .. clientId
        .. '&client_secret=' .. clientSecret
              .. '&username=' .. username
        .. '&password=' .. password
    local response_body = {}
    local _, code, _, _ = https.request{
        url = authEndpoint,
        method = 'POST',
        headers = {
            ['Content-Type'] = 'application/x-www-form-urlencoded',
            ['Content-Length'] = #requestBody
        },
        source = ltn12.source.string(requestBody),
        sink = ltn12.sink.table(response_body)
    }
end
  log(response_body, code, AccessToken, source, sink))


Has anyone managed to integrate ttlock locks?

Thank you very much in advance.


RE: API ttlock - admin - 08.02.2024

Use encodepost to correctly create a POST request body: https://forum.logicmachine.net/showthread.php?tid=4286&pid=27711#pid27711

In your example getAccessToken() is not called anywhere. log() must be inside of the getAccessToken() function. Otherwise certain variables like response_body won't be logged because they are local to the function and are not visible outside of it.