how to authenticate to a HTTPS server / integrate with pushbullet?

How would one authenticate and hit a HTTPS URL from LUA? In particular to get the resultant response back from the server?

Background: Basically want to use the Pushbullet API API. I can see how to use it in curl, however I want to use it within LUA, and really want to get the responses back to see them.

For example in curl on command line it is:

curl -u <your_access_token_here>: -X POST https://api.pushbullet.com/v2/pushes --header 'Content-Type: application/json' --data-binary '{"type": "note", "title": "Note Title", "body": "Note Body"}

So in other words would like to work out how to implement the above in LUA instead, such that I can get the response (string) back from the server.

https://www.google.com/search?q=site%3Aforum.micasaverde.com+lua+socket+https

  • Garrett

I’ve got to this point but getting a 401 response header (unauthorized) - are there any obvious issues with my code/syntax below? (I’ve changed the access token) API reference for pushbullet is here: Pushbullet API

OUTPUT

Debugging session completed (traced 0 instructions). Program completed in 2.00 seconds (pid: 7148). "ONE:" 1 "Code:" 401 "Header" "connection" "close" "Header" "www-authenticate" "Basic realm=\"Pushbullet\"" "Header" "date" "Wed, 10 Dec 2014 09:05:04 GMT" "Header" "content-type" "application/json; charset=utf-8" "Header" "x-ratelimit-remaining" "16382" "Header" "cache-control" "private" "Header" "x-ratelimit-limit" "16384" "Header" "x-ratelimit-reset" "1418204524" "Header" "server" "Google Frontend" "Status:" "HTTP/1.1 401 Unauthorized"

CODE:

[code]
https = require(“ssl.https”)

local res = {}
local data = [[
{
“type”: “link”,
“title”: “Link Note Title”,
“body”: “Note Body”,
“url”: “http://google.com/
}
]]
local headers = {
[“Authorization”] = “Basic ASDFASDFASDFASDFASDF”,
[“content-length”] = string.len(data),
[“content-type”] = “application/json”
}
local url = { url = “https://api.pushbullet.com/v2/pushes”,
protocol = “tlsv1”,
method = “POST”,
source = ltn12.source.string(data),
sink = ltn12.sink.table(res),
headers = headers }

local one, code, headers, status = https.request(url)

print(“ONE:”, one)
print(“Code:”, code)
for k,v in pairs(headers) do
print(“Header”, k, v)
end
print(“Status:”,status)[/code]

I’ll have to take it on your honour that you’ve built the access token properly: join the username, a literal colon, and the password into a single string, then pass it through mime.b64().

Something else to try is to see if the pushbullet service tolerates HTTP header names in lower case. We discovered this on the MCV forum: http://forum.micasaverde.com/index.php?topic=13081.0

thanks for the info

Update - Actually heard back from Chris at pushbullet which was great. He noted Basic auth was a bit more complicated (guessing I may have needed to do the 2 step process), however that “We also allow Bearer auth”.

So in summary changing my code with the following line made things work:

   ["Authorization"] = "[b]Bearer[/b] ASDFASDFASDFASDFASDF",

[quote=“mixedup, post:5, topic:184588”]thanks for the info

Update - Actually heard back from Chris at pushbullet which was great. He noted Basic auth was a bit more complicated (guessing I may have needed to do the 2 step process), however that “We also allow Bearer auth”.

So in summary changing my code with the following line made things work:

   ["Authorization"] = "[b]Bearer[/b] ASDFASDFASDFASDFASDF",[/quote]

Thanks for your work, I’ve been looking for a way to send notifications to Pushbullet for quite some time, and with this code it worked! I don’t know if you are capable of doing it, but maybe you can turn this in to a plugin for Vera?

no time…not sure it would make sense alone, perhaps if someone with a notifications plugin wanted to incorporate it as an option that would

Hit up Richard of the VerAlerts plugin. He may be able to add it to the plugin.

  • Garrett

I would consider adding it to Vera Alerts if there are enough people interested in this notification service.

+1 to Vera Alerts please Richard!

I recently added a function to the Lua Startup to use for sending pushes via PushBullet, this also includes a url_encode function to make URL’s work properly. Currently the device identity is hard coded but it could be added as a parameter. Happy to provide the code if anyone wants to use it.

Added to VeraAlerts Version 6.31