Logic Machine Forum
Curl Request to Lua - 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: Curl Request to Lua (/showthread.php?tid=2354)



Curl Request to Lua - ralwet - 16.11.2019

Hi

Due to the changes of apixu doing their new api, I want to implement another API from our local provider in Switzerland (https://developer.srgssr.ch/content/quickstart-guide).

Curl-Request is working. But I do struggle implementing the Curl-Call in Lua.

Working Curl:

Code:
curl -v -X POST 'https://api.srgssr.ch/oauth/v1/accesstoken?grant_type=client_credentials'   \
-H 'Authorization: Basic RFJNblFyV1lpRnN3VGVyS1pOVWpJUzFOQ00WDhHUzU6THlsMG5udGdoMXo2VWxxeg=='   \
-H 'Cache-Control: no-cache'   \
-H 'Content-Length: 0'   \
-H 'Postman-Token: 24264e32-2de0-f1e3-f3f8-eab014bb6d76'

The Curl-Request returns successfully a new bearer access-Token. Verbose-Response from Curl:

Code:
*   Trying 3.120.59.111...
* TCP_NODELAY set
* Connected to api.srgssr.ch (3.120.59.111) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* successfully set certificate verify locations:
*   CAfile: /etc/ssl/certs/ca-certificates.crt
  CApath: /etc/ssl/certs
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* TLSv1.3 (IN), TLS handshake, Server hello (2):
* TLSv1.2 (IN), TLS handshake, Certificate (11):
* TLSv1.2 (IN), TLS handshake, Server key exchange (12):
* TLSv1.2 (IN), TLS handshake, Server finished (14):
* TLSv1.2 (OUT), TLS handshake, Client key exchange (16):
* TLSv1.2 (OUT), TLS change cipher, Client hello (1):
* TLSv1.2 (OUT), TLS handshake, Finished (20):
* TLSv1.2 (IN), TLS handshake, Finished (20):
* SSL connection using TLSv1.2 / ECDHE-RSA-AES256-GCM-SHA384
* ALPN, server accepted to use http/1.1
* Server certificate:
*  subject: OU=Domain Control Validated; CN=*.api.srgssr.ch
*  start date: Oct 21 15:11:03 2019 GMT
*  expire date: Oct 21 08:04:26 2021 GMT
*  subjectAltName: host "api.srgssr.ch" matched cert's "api.srgssr.ch"
*  issuer: C=US; ST=Arizona; L=Scottsdale; O=GoDaddy.com, Inc.; OU=http://certs.godaddy.com/repository/; CN=Go Daddy Secure Certificate Authority - G2
*  SSL certificate verify ok.
> POST /oauth/v1/accesstoken?grant_type=client_credentials HTTP/1.1
> Host: api.srgssr.ch
> User-Agent: curl/7.58.0
> Accept: */*
> Authorization: Basic RFJNblFyV1lpRnN3VGVyS1pOVWpJUzFOQU00WDhHUzU6THlsMG5udGdoMXo2VWxxeg==
> Cache-Control: no-cache
> Content-Length: 0
> Postman-Token: 24264e32-2de0-f1e3-f3f8-eab014bb6d76
>
< HTTP/1.1 200 OK
< Date: Sat, 16 Nov 2019 13:47:37 GMT
< Content-Type: application/json
< Content-Length: 546
< Connection: keep-alive
< Access-Control-Allow-Origin:
< Access-Control-Allow-Headers: origin, x-requested-with, accept, content-type, Authorization
< Access-Control-Max-Age: 3628800
< Access-Control-Allow-Methods: GET
<
{
  "refresh_token_expires_in" : "0",
  "api_product_list" : "[SRG-SSR-PUBLIC-API-V2]",
  "api_product_list_json" : [ "SRG-SSR-PUBLIC-API-V2" ],
  "organization_name" : "srgssr",
  "developer.email" : "s@ss.ch",
  "token_type" : "BearerToken",
  "issued_at" : "1573912057331",
  "client_id" : "DRMnQrWYiSswTerKZNUDIS1NAM4XwGS5",
  "access_token" : "MATOKvdJZLtjJy6EyjlAKm7Tk7Zu",
  "application_name" : "7c0dd30c-70a0-494f-b9b0-ed7c0b704fad",
  "scope" : "",
  "expires_in" : "2591999",
  "refresh_count" : "0",
  "status" : "approved"
* Connection #0 to host api.srgssr.ch left intact

Now my Code in Lua:

Code:
require('json')
require('ssl.https')
require('ltn12')
require('encdec')

local response_body = {}
local request_body = ""
local body, code, hdrs, stat = ssl.https.request({
  url = 'https://api.srgssr.ch/oauth/v1/accesstoken?grant_type=client_credentials',
  sink = ltn12.sink.table(response_body),
  method = 'POST',
  headers = {
    ["Authorization"] = "Basic RFJNblFyV1lpRnN3VGVyS1pOVWpJUzFOQ00WDhHUzU6THlsMG5udGdoMXo2VWxxeg==",
    ["Cache-Control"] = "no-cache",
    ["Content-Length"] = 0,
    ["Postman-Token"] = "24264e32-2de0-f1e3-f3f8-eab014bb6d76"
  },
  source = ltn12.source.string(request_body)
})

log(body)
log(code)
log(hdrs)
log(stat)

Logs are returning

Code:
body: * nil
code: * string: closed
hdrs: * nil
stat: * nil

Can anybody see, why my Lua-Call is failing with "* nil"?

Thank you! Smile


RE: Curl Request to Lua - admin - 16.11.2019

Try installing newer firmware, older one uses SSLv3 by default for HTTPS which is not supported by most servers now.


RE: Curl Request to Lua - ralwet - 16.11.2019

I'm using a SpaceLynk HW3 (i.MX6) with firmware Version 2.4.0. As far as I know this is already the latest available firmware (https://www.se.com/ww/en/product/LSS100200/spacelynk-logic-controller).

Is this SpaceLynk firmware still using SSLv3?