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.

Solis inverter for solar panels
#2
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.
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)
Reply


Messages In This Thread
RE: Solis inverter for solar panels - by admin - 20.05.2022, 14:31
RE: Solis inverter for solar panels - by Joep - 20.12.2022, 08:28
RE: Solis inverter for solar panels - by Joep - 15.01.2023, 12:34

Forum Jump: