I'm going to start by saying I started learning Python about 2 weeks ago and I'm not a programmer by profession. So, I've got a Pi 3 and am playing with some home automation stuff. Specifically, I'm trying to get the Pi to control my Hive devices using their badly documented API.
I'm using Flask and have routes to login, store the access token in a session variable, check the existence of the variable, etc.
The only way I can find to check the login status is to send a simple request and see if it comes back with an error. This works if I hard code the token into the JSON.
This is what I have:
url = "https://api.prod.bgchprod.info:443/omnia/users"
hiveSessionId = session['hiveSessionId']
print hiveSessionId
headers = {
'Content-Type': "application/vnd.alertme.zoo-6.1+json",
'Accept': "application/vnd.alertme.zoo-6.1+json",
'X-Omnia-Client': "Hive Web Dashboard",
'X-Omnia-Access-Token': "{hiveSessionId}",
'Cache-Control': "no-cache",
}
print headers
response = requests.request("GET", url, headers=headers)
data=response.json()
print(response.text)
if 'errors' in data:
return "Not logged in"
return "Logged in"
It's the bit that says:
'X-Omnia-Access-Token': "{hiveSessionId}",
that I'm struggling with. I've tried various different permitations of double quotes, single quotes, escaped, etc - all found on stackexchange.
What I get when I run this is:
B4QpAIxAzIebkSKCQFCIjwQlALaLt
{'X-Omnia-Client': 'Hive Web Dashboard', 'Accept': 'application/vnd.alertme.zoo-6.1+json', 'X-Omnia-Access-Token': '{hiveSessionId}', 'Cache-Control': 'no-cache', 'Content-Type': 'application/vnd.alertme.zoo-6.1+json'}
{"errors":[{"code":"NOT_AUTHORIZED"}]}
So the session id is there. I thing it's just the syntax for inserting the string into the JSON header that I'm getting wrong.
Any help would be gratefully received.
Cheers Andy