Unifi Protect binding - HTTP protocol violation: Authentication challenge without WWW-Authenticate header

I’m afraid that you can’t simply copy code found online :wink:

The clue is that you need to deactivate said protocol, and the logical place to do it is where the HttpClient is created, in my opinion. The HttpClient instance is passed around a lot, but I think I found where it originates from (UniFiProtectNvr).

Also, you shouldn’t try to start the client more than once. The important part of the referenced post is that you must remove the protocol after the client has been started, so why not put it directly after it has been started?

This is my suggested change:

 .../openhab/binding/unifiprotect/internal/model/UniFiProtectNvr.java    | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/src/main/java/org/openhab/binding/unifiprotect/internal/model/UniFiProtectNvr.java b/src/main/java/org/openhab/binding/unifiprotect/internal/model/UniFiProtectNvr.java
index bbc7c41..fcddcdd 100644
--- a/src/main/java/org/openhab/binding/unifiprotect/internal/model/UniFiProtectNvr.java
+++ b/src/main/java/org/openhab/binding/unifiprotect/internal/model/UniFiProtectNvr.java
@@ -22,6 +22,7 @@ import java.util.Optional;
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
 import org.eclipse.jetty.client.HttpClient;
+import org.eclipse.jetty.client.WWWAuthenticationProtocolHandler;
 import org.eclipse.jetty.util.ssl.SslContextFactory;
 import org.openhab.binding.unifiprotect.internal.UniFiProtectIrMode;
 import org.openhab.binding.unifiprotect.internal.UniFiProtectLcdMessage;
@@ -93,6 +94,7 @@ public class UniFiProtectNvr {
         try {
             httpClient.start();
             logger.info("Initializing the binding, with config: {}", getConfig());
+            httpClient.getProtocolHandlers().remove(WWWAuthenticationProtocolHandler.NAME);
         } catch (Exception e) {
             logger.error("Failed to start binding: ", e);
             return false;
1 Like