Using Lua Socket to Communicate with the Cloud API

Hello All!
I’m new to the Particle Forum but I have been using Particle products for a while. I have been trying to write a program in Lua to communicate through the Cloud API.

I am Using ZeroBrane Studio as an the Lua IDE and Lua Socket. My code is below:

-- load the http module
local http = require("socket.http")

local headersvars = { {"grant_type", "password"}; {"username", "myemail"}; {"password", "mypass"}; {"expires_in", "0"};}

local result, respcode, respheaders, respstatus = http.request {
  url = "",
  method = "POST",
  user = "myclientid",
  password = "mysecret",
  headers = headersvars,

I cant even get it to connect to the Site. My output is below:

host or service not provided, or not known

Hmm… Are you trying to create a new customer scoped access token?

Yes, I am trying to replicate small parts of the Windows SDK for my own use and I’m starting with generating an access token. I’m working on a Prototype Product.

So with further experimentation I have gotten this far:

local socket = require("socket")
local http = require("ssl.https")
local ltn12 = require("ltn12")
http.TIMEOUT = 5

local resultTable = {}

local result, respcode, respheaders, respstatus = http.request {
  url = "",
  sink = ltn12.sink.table(resultTable),
  method = "POST",
  user = "MyClientID",
  password = "My Something Secret",
  headers = {
    grant_type = "password",
    username = "MyUsername",
    password = "MyPassord",

for s,i in pairs(resultTable) do

and the Result is:

HTTP/1.1 400 Bad Request
1	{"error":"invalid_request","error_description":"Method must be POST with application/x-www-form-urlencoded encoding"}

Now I have Read up on this and it says a 400 Request means : Your request is not understood by the device, or the requested subresource (variable/function) has not been exposed.