Accessing items via rest API through myopenhab using OAuth 2

Hi,
I’m using alexa skill to interact with my openHAB installation via myopenhab.org. It works great but due to current limitations of the smart home alexa model, I’m not able to get all the answers I’d like from my smart home. For example, AFAIK there is no way to get current power consumption or you need to implement workarounds.
One of the workarounds I used was to attach a rule to the LastVoiceCommand event to parse the question and TTS out a speech with the current power consumption. But alexa doesn’t stop processing the command so what I get as a reply is something like “I don’t know…” interrupted and then “Your power consumption is currently 2.3 kW”. Not nice.
So - even if I know nothing about node.js - I tried to write my personal alexa skill in order to ask questions like “Alexa, ask Mister John what the current power consumption is”.

So my problem now is that I’m not able to perform OAuth 2 authentication to myopenhab.org as I’m not sure what client ID / secret pair I should use to authenticate the skill.

Is this Client ID / secret pair something that has to be stored in myopenhab.org? Any way to create one?

I think this not related to the request coming from an alexa skill, it could be any other UI on the web trying to access the apis,

thx

Check if OAuth2 using just OH Rules and myopenhab.org helps.
Alternatively you may try to access the REST API to read out your item. Authentication can be done by using email address and your password. I am doing that to access my own openhab-cloud instance.

1 Like

My understanding is that this article talks about openHAB accessing external APIs not the opposite, am I wrong?

I tried this one but it doesn’t seem to work with myopenhab instance.

Could you describe a bit more in detail what you tried and what does not work ?
In case you login manually to myopenhab instance do you get access to the /rest/items via browser ?
What also should be possible is to run an alexa skill locally e…g. on openhab instance and access the /rest/items on your local server.

I write the encoded URL in the web browser and I’prompted to provide credentials or I get e message Unauthorized. I tried

https://<email address>:<password>@home.myopenhab.org
https://<email address>:<password>@myopenhab.org

With or without https or http.
I couldn’t make it work even in the browser.

Yes all items are accessible.

Try this one:

curl -q -o - --user  '<email address>:<password>' https://<hostname>/rest/items/<itemname>

The result that will be returned via STDOUT will be json formatted.

1 Like

mmm, it works from my raspberry but it doesn’t from Windows command line (my Win pc is in the same LAN as my RBPI). I get “Unauthorized” from the Windows command line.
I also tried this node.js script in the alexa skill

function httpGet() {
return new Promise(((resolve, reject) => {
    var options = {
        host: 'myopenhab.org',
        path: '/rest/items/Energy_PowerConsumption/state',
        method: 'GET',
        headers: {
            'Authorization': 'Basic <username>:<password>'
        },
    };

    const request = https.request(options, (response) => {
        response.setEncoding('utf8');
        let returnData = '';

        response.on('data', (chunk) => {
            console.log("**CHUNK");
            returnData += chunk;
        });

        response.on('end', () => {
            console.log("**END");
            resolve(returnData);
        });

        response.on('error', (error) => {
            console.log("**ERROR");
            reject(error);
        });
    });
    
    request.end();
}));

}

and I get “Unauthorized” as a result.
Any ideas?

EDITED
changing the ‘authorization’ option in node.js it works now:

headers: {
    'Authorization': 'Basic ' + new Buffer('<username>:<password>').toString('base64')
},

still don’t understand why it doesn’t in windows command line but probably I don’t care.
thanks @Wolfgang_S !!!

Try to use double quotes ( " ) instead of single ( ’ ) quote on windows command line.
I just copied the example from my Linux instance without taking into account that the single quotes could be a problem on windows system…

1 Like