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.

Has someone already integrated LG ThinQ
#5
Here is my code so far.

I get this error when executing the code:

LG Thinq 08.05.2023 14:35:56
Library socket/http:0: bad argument #1 to 'escape' (string expected, got function)
stack traceback:
[C]: in function 'request'
Resident script:31: in function 'auth_request'

Code:
require('socket.http')
require('encdec')

OAUTH_SECRET_KEY = "c053c2a6ddeb7ad97cb0eed0dcb31cf8"
OAUTH_CLIENT_KEY = "LGAO221A02"
DATE_FORMAT = "!%a, %d %b %Y %H:%M:%S"
V2_AUTH_PATH = "/oauth/1.0/oauth2/token"

function oauth2_signature(message, secret)
  local hashed = encdec.hmacsha1(message, secret, true)
  return encdec.base64enc(hashed)
end

function auth_request(oauth_url, reqdata)
    if string.sub(oauth_url, -1):lower() == "/" then  oauth_url = string.sub(oauth_url, 0, #oauth_url-1) end
    local url = oauth_url .. V2_AUTH_PATH
    local timestamp = tostring(os.date(DATE_FORMAT, os.time())) .. " +0000"
    local data_list = {}
    for k, v in pairs(reqdata) do  table.insert(data_list, string.format("%s=%s", k, v)) end
    local req_url = string.format("%s?%s", V2_AUTH_PATH, table.concat(data_list, "&"))
    local message = string.format("%s\n%s", req_url, timestamp)
    local sig = oauth2_signature(message, OAUTH_SECRET_KEY)
    local headers = {
        ["x-lge-appkey"] = OAUTH_CLIENT_KEY,
        ["x-lge-oauth-signature"] = sig,
        ["x-lge-oauth-date"] = timestamp,
        ["Content-Type"] = "application/x-www-form-urlencoded",
        ["Accept"] = "application/json"
    }
    castdata = table.concat(data_list, "&")
    socket.http.request(url,{
        options = {
            headers = headers,
            data = castdata,
            method = "POST",
            timeout = 5000,
        },
        success = function(response)
            log("auth_request success", response.data)
        end,
        error = function(error)
            log('error: ' .. json.encode(error))
        end
    })
end
   
local requestBody = {
    grant_type = "refresh_token",
    refresh_token = "4ed29cb48cc1cb6228658d50817b789d67d37dbad56bf561cc6389a4ab69070d8ee808edb3377ad3b07e243c467f1d36"
}

auth_request("https://us.lgeapi.com/", requestBody)
Reply


Messages In This Thread
RE: Has someone already integrated LG ThinQ - by Joep - 08.05.2023, 12:46

Forum Jump: