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.

API ttlock
#1
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.
Reply
#2
Use encodepost to correctly create a POST request body: https://forum.logicmachine.net/showthrea...1#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.
Reply


Forum Jump: