20.05.2022, 14:31
Try this, change key_id/key_secret as needed. request function accepts two parameters - request URI (starting with /v1/api/...) and request body (either a table which is converted to JSON or a string which is sent as is).
The calculated signature differs from documented example even though it is calculated the same way. Might be an error in the documentation.
The calculated signature differs from documented example even though it is calculated the same way. Might be an error in the documentation.
Code:
require('json')
require('encdec')
require('socket.http')
require('ltn12')
host = 'https://www.soliscloud.com:13333'
key_id = '2424'
key_secret = '6680182547'
function request(uri, body)
local method = 'POST'
if type(body) ~= 'string' then
body = json.encode(body)
end
local content_md5 = encdec.md5(body, true)
content_md5 = encdec.base64enc(content_md5)
local content_type = 'application/json'
local date = os.date('!%a, %d %b %Y %X GMT')
local sign = method .. '\n' ..
content_md5 .. '\n' ..
content_type .. '\n' ..
date .. '\n' ..
uri
sign = encdec.hmacsha1(sign, key_secret, true)
local url = host .. uri
local auth = 'API ' .. key_id .. ':' .. encdec.base64enc(sign)
local headers = {
['Content-MD5'] = content_md5,
['Content-Type'] = content_type,
['Date'] = date,
['Authorization'] = auth,
['Content-Length'] = #body,
}
local resp = {}
local res, code = socket.http.request({
url = url,
method = method,
source = ltn12.source.string(body),
sink = ltn12.sink.table(resp),
headers = headers
})
if res and code == 200 then
return table.concat(resp)
else
return nil, code
end
end
res, err = request('/v1/api/userStationList', { userId = '1145611319416590338' })
log(res, err)