HomeHome AutomationEventGhost basic authentication in HTTP requests

EventGhost basic authentication in HTTP requests

More magic in EventGhost

I’m sharing this tip, as it took me way too long to find a simple solution online, and as most of my articles, as a reminder of how to do this again in the future. After increasing security on my NodeRED server and certifying the server, I had to learn how to authenticate the HTTP requests with username and password and how to deal with HTTP vs HTTPS requests.

EventGhost basic authentication in HTTP|HTTPS requests

I’m submitting my data to the NodeRED server, which is password protected and SSL certified. This means I have to now include the basic authentication (username, password) in my few requests. I have put together a few lines of codes that make it easy for you guys to use, and obvious to read.

HTTP

To send the request – use the Python Script action and use the code below:

//HTTP POST request
import requests

username = "yourusername"                   ## your HTTP username
password = "secretpasswored"                ## your HTTP password
url = "http://192.168.1.xxx:1880/path"     ## NodeRED server address
data = {"light":1}                          ## some JSON formatted data

resp = requests.post(url, data, auth=(username, password))

HTTPS

To send the request – use the Python Script action and use the code below:

import requests
import warnings

#comment out the below line to get the Python warnings back
warnings.filterwarnings("ignore")

#set your credentials data
url = "https://192.168.1.xxx:1880/path"
username = "your username"
password = "your passowrd"
data = {"light":1}

#request the data & print it (comment out print later)
resp = requests.post(url, data, auth=(username, password), verify=False)

The code is simple, but if you are using a laptop, consider DNS rather than the IP for the NodeRED server. If you want to know why or what can you do with it, imagine linking a request like this to a keyboard macro.  You can use this to trigger lights, events or send notifications to other devices.

EventGhost Global Variables

Because the details of my server have changed, I had to redo all my HTTP requests. I don’t want to do this each time something changes. I figured out that I can set the global variables in EventGhost on start. Let’s do this so should anything happen I can change the values in a single script.

On boot, I want EvenGhost to load the default values which will be used later on in each script. We can do this by placing a Python script with the sensitive details in AutoStart:

#request the data & print it (comment out print later)
credentials = requests.get(url, auth=(username, password), verify=False)
print credentials.content

data = json.loads(credentials.content)


#Get JSON data  python username = data['JSON key']['key'] ie
#AutoRemote ID is stored as {"AR": "someID" }
#HTTP URL is stored in {"HTTP" :{"server":{"request":"URL FOR REQUEST"}}}

pixel = data['AR']
http = data['HTTP']['server']['request']

#store as global variable "test"|"test2", value
setattr(eg.globals, "test", pixel)
setattr(eg.globals, "test2", http)

print "***Default values loaded***"

These values will be loaded when EventGhost starts and the same HTTP post from before will this time look like that:

import requests
#setting username and password "HTTP_username"|"HTTP_password"|"HTTP_ip" are
#the name of the global variable
username = getattr(eg.globals, "HTTP_username")
password = getattr(eg.globals, "HTTP_password")

#composing a correct URL for HTTP
path = "/sonoff/relay/"
url = getattr(eg.globals, "HTTP_ip") + path

#adding data
data = {"light":0}

#sending a HTTP POST
resp = requests.post(url, data, auth=(username, password))

This way, if your IP or credentials change you have to modify only a single file.

I hope this tip on how EventGhost basic authentication in HTTP requests works will cut the googling time for you. If I can’t find a simple and well-explained answer within 3 pages of google searches it deserves an article. Even if it’s going to be a short one.

Project Download

Download project files here. Bear in mind that Patreon supporters have early access to project files and videos.

PayPal

Nothing says "Thank you" better than keeping my coffee jar topped up!

Patreon

Support me on Patreon and get an early access to tutorial files and videos.

image/svg+xml

Bitcoin (BTC)

Use this QR to keep me caffeinated with BTC: 1FwFqqh71mUTENcRe9q4s9AWFgoc8BA9ZU

Smart Ideas with

Automate your space in with these ecosystems and integrate it with other automation services

client-image
client-image
client-image
client-image
client-image
client-image
client-image
client-image
client-image

Learn NodeRED

NodeRED for beginners: 1. Why do you need a NodeRED server?

0
To server or not to server? That's a very silly question!

Best Automation Projects

Tuya SDK for beginners: Intro to Tuya Cloud API

0
Working with Tuya Cloud API. A guide to Cloud automation for beginners, get started with REST!

NEST your old thermostat under $5

0
Nest-ing up your older thermostat under $5

Nora – Google Assistant in NodeRED

0
Integrate Google Assistant with NodeRED thanks to Nora - NodeRED home automation

Sonoff Zigbee Bridge – review

0
Sonoff line up will soon include Sonoff Zigbee Bridge and more Zigbee sensors - here is the first look

DIY Smart Washing Machine – for about 15 bucks!

0
Learn how to add washing machine notifications to your Google Home on the cheap

Smart Home

We’ve seen this before: SwitchBot K10+ PRO

0
This is an odd one. Building on the success of SwitchBot K10+ they released SwitchBot K10+ PRO - but is the experience actually better?

Is this the smart panel we have waited for?

0
ITEAD has released a new smart panel: Sonoff NSPanel Pro 120 - have they learned the lesson from the terrible launch of the original Pro? Let's see what's new.

Aqara FP1E detects motionless humans

0
This isn't exactly a new device, it's an interaction of the original Aqara presence sensor. Aqara FP1E brings Matter, ZigBee and new triggers to your smart home

SwitchBot S10: cleaning re-imagined!

0
SwitchBot S10 promises unattended vacuuming and mopping so you can focus on things you love and care for. Does it deliver?

Sonoff for households and gardens

0
ITEAD released two ZigBee devices to manage water in your household and gardens. Now you can monitor the flow and dispense water with Sonoff SWV and prevent water damage using the Sonoff Water Leak sensor with a twist.