Visual Code and Openhab 3.0

Hi,
fist of all thanks for your help. Really appreciated!

@Dynamyte
No difference for me (I had already tried that, but I gave it another go anyway)

@Confectrician
I have tried the correct syntax and I still get the 401 error code
Just to be clear I used the credentials I use to login as an admin into the new home page.
I have also removed the special characters ($) I was using in my password (just in case)

What I have discovered though that the API do not require authorization eg:
http://192.168.31.2:8080/rest/
shows all the APIs available

Are you using any kind of reverse proxy in your setup?

I have problems accessing the rest api with this user credential link too, when i open it in browser,
because nginx reverse proxy will reqrite my request.

But doing it with a special api client like postman worked for me with basic auth.
I have tested it especially for you before posting the questions above.

I have one last idea to get the problem sorted out a bit at least.
You could temporary disable the whole authentication for rest api via openHAB console.

When you are able to connect this way with the vscode extension (just host and port configured not user and passsword). We have to start looking at the authentication again.
If there is still errors we have to check again in the extension configuration first.

I am running Openhabian which I installed from scratch yesterday morning on pi Pi4.
It is a standard setup and I have not set any reverse proxy (that I know of).

So as I said earlier I realised that I did not need authorization to access the api, therefore I have deleted Username and Password in the VS Code settings and now I can connect (??)
I have Basic Authentication enabled

I am confused but can work for the moment

With the settigns shown above you can access the api via the implicit user role.
There may be api actions that are still restricted since they do not belong to the “user role”

Similar problem here. With the following settings:

"openhab.host": "http://openhab",
"openhab.port": 8080,
"openhab.username": "Admin",
"openhab.password": "iwonttellyoumypassword"

No matter whether implicit user roles are enabled or disabled. Port 5007 (LSP) does not work either, which one should I use? It also does not work with an API token instead of a password (I would usually prefer the first option).

I always receive the following error message from the openHAB extension:

Error while connecting to openHAB REST API. self signed certificate

And loads of ENOTFOUND and parse error in the OH Language Server output in VSC.

I also found this related thread, but it still seems to be unresolved: [OH3] REST API Authentication Help.

So you are using openhab with https?
Probably behind a reverse proxy?

Edit:
Token based authentication won’t work with the extension for now.
I have stated that already somewhere but will add it to my FAQ thread now.

So you are using openhab with https?
Probably behind a reverse proxy?

No, I haven’t set up anything like this. I wish I could, but some months ago this looked far too sophisticated to me.

Any other ideas? So this should work in theory?

In theory it should work with an http connection yes.

Could you please try to remove the http:// from your config for testing in the openhab.host setting.
Second try would be to enter the ip adress directly instead of the hostname.

Alltough i doubt that it will work with a self signed certificate error.

Success! Turns out that you have to reload the VS Code window after every change you have made to the settings. This is a bit counter-intuitive, I have just left an issue for this in the extension repository.

To sum up, the following configuration now works for me:

"openhab.host": "openhab",
"openhab.port": 8080,
"openhab.username": "Admin",
"openhab.password": "iwonttellyoumypassword"

I’m looking forward to the day when you will be able to use tokens for authentication – it feels kind of unsafe to store my password in plain text, especially because I have turned on settings sync via the Microsoft cloud … :slight_smile:

2 Likes

Yeah maybe we could solve that if we reload the window on puprose after a config change.
Maybe we can subscribe to a config change via api and then reload settings.
But cleanest way would probably be to reload the window and activate the extension from scratch.

Anyway huge thanks for bringing it to github directly. :+1:

1 Like

If you create an API token specifically for VS code (that’s the point of these tokens, you dedicate them to specific services so you don’t have to share your master password around), you should be able to use it as your username if you leave the password blank.

2 Likes

Yeah maybe we could solve that if we reload the window on puprose after a config change.

Nah, rather not, there are too many other extensions that do not properly resume after the window has been reloaded. I think the cleanest solution would just be to reload the settings every time manually – how expensive can this be today? However, probably we should move this discussion to GitHub. :slight_smile:

1 Like

Ah, thanks a lot! I very recently found out something similar, see this post:

(However, I don’t understand why you have to specify the access token as user but not as password! I think the common solution is to enter it as password instead. Is OH really following the (in)official standards here?)

Simple answer. The extension currently is capable of handling basic auth and the extension code omits the :password when you just enter a username.
That’s why it has to be in the password field (currently).

Th “latest” vscode extension release is from January so don’t expect too much openHAB 3 adaptions currently.

Good question, some software doesn’t allow empty user names and the token identified the user so I made it the “username” - but on the other hand some other software will log the password in plain text so you can inadvertently leak it. Maybe both options should work…

Yes, I also think both ways should be supported :+1: Many clients will treat the password as a more sensible data than the username.

ADMIN : NO advertising here !!!

Hello everyone ive just merged to an openhabian pi from my previous widows setup.
I was really struggeling with getting Visualcode to connect. (authentication failure)
AT LAST i changed my OH admin username (users.json) that had an emailadress with an @ to simple “admin”. I dont think OH/visualcode appriciates @ in the username

http://MYUSER:MYPSWD@192.168.31.2:8080/rest/things also trow an authentication fail with my email as username (webui worked fine)

Sucsess!

That’s not OH or vscode, it is the url format that uses @ as a delimiter to separate userinfo from the host. So every (correct working) application thinks " ah yes that’s the end of the user-data. here come the host".

Thank you for the reply
Maybe code first setup of username and passord not to allow @ :slight_smile:
Just wanna give a heads up if anyone else do this mistake migrating to OH3

Are you sure?
Yes, the username inside an URL must not contain an @.

But I would have expected this to work:

"openhab.username": "admin@example.com",