0

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

2 Answers 2

1

Typical!

10 seconds after I post here, after messing around for about 3 hours, I find the answer. Experts will know but here's what I did for anyone else looking:

'X-Omnia-Access-Token': hiveSessionId,

Simple as that.

Cheers Andy

Sign up to request clarification or add additional context in comments.

Comments

0

You already found the answer, congrats:) Also I can recommend you to use Python 3 instead 2. You can use some useful features like f-strings literals:

'X-Omnia-Access-Token': f"{hiveSessionId}",

Of course for this task it's redundantly, but maybe you would find it attractive:)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.