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 = "https://api.particle.io/oauth/token",
method = "POST",
user = "myclientid",
password = "mysecret",
headers = headersvars,
}
print(result)
print(respcode)
I cant even get it to connect to the Site. My output is below:
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 = "https://api.particle.io/oauth/token",
sink = ltn12.sink.table(resultTable),
method = "POST",
user = "MyClientID",
password = "My Something Secret",
headers = {
grant_type = "password",
username = "MyUsername",
password = "MyPassord",
},
}
print(result)
print(respcode)
print(respstatus)
for s,i in pairs(resultTable) do
print(s,i)
end
and the Result is:
1
400
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.