mmaelzer/mjpeg-consumer

401 - Unauthorized

MarkSweat opened this issue · 3 comments

I'm trying to use your module with a Samsung network camera without success. I'm using a SND 1080N model with SUNAPI 2.2.1.

In another issue, I saw that you said to try piping to a file, "raw.txt" to see what is returned from the camera. When I do that I see an HTML page returned from the camera with error 401-Unauthorized. I'm following your example code such as,

var request = require('request');
var fs = require('fs');
var MjpegConsumer = require("mjpeg-consumer");
var FileOnWrite = require("file-on-write");

var writer = new FileOnWrite({
path: './video',
ext: '.jpg'
});
var consumer = new MjpegConsumer();

var username = "user1";
var password = "Password_1";
var options = {
url: "http://192.168.2.104/stw-cgi/video.cgi?msubmenu=snapshot&action=view",
headers: {
'Authorization': 'Basic ' + new Buffer(username + ':' + password).toString('base64')
}
};

request(options).pipe(fs.createWriteStream("raw.txt"));

I've also tried changing the request to other commands such as,

request("http://user1:Password_1@192.168.2.104/stw-cgi/video.cgi?msubmenu=snapshot&action=view).pipe(fs.createWriteStream("raw.txt"));

If I enter http command enter Chrome with the auth info it works. Such as
http://user1:Password_1@192.168.2.104/stw-cgi/video.cgi?msubmenu=snapshot&action=view

Any ideas what's going on?

I fiddled around with a bunch of different was to send the request. These included using "http.createClient" and "http.get" and still got the 401 error.

What worked was,

request.get('myURL').auth('username', 'password', false).

It sounds like your camera is configured to accept digest authentication. My basic authentication example would definitely not work in that case whereas what you found to work, obviously, does.

For digest authentication, you can slightly modify my example like so:

var options = {
  url: "http://192.168.2.104/stw-cgi/video.cgi?msubmenu=snapshot&action=view",
  auth: {
    user: "user1",
    pass: "Password_1",
    sendImmediately: false
  }
};
request(options).pipe(consumer).pipe(writer);

The key here is that sendImmediately is set to false so that the request will retry with a proper authentication header after receiving a 401 response from the server.

Hope this clarifies things. I'm going to go ahead and close this issue as the mjpeg-consumer library actually doesn't handle any http request logic.

Mike, Thanks for your clarification on authorization. Once I got past that part of the setup, your module worked flawlessly.

--Mark