agsh / onvif

ONVIF node.js implementation
http://agsh.github.io/onvif/
MIT License
692 stars 234 forks source link

Snapshot - 401 unauthorized #190

Closed Wade-BuildOtto closed 3 years ago

Wade-BuildOtto commented 3 years ago

Has anyone solved the issue when requesting a snapshot with various methods of passing in username and password, I get a 401 unauthorized, even going to the url for the cgi and filling in the popup it just ask over and over again.

RogerHardiman commented 3 years ago

There is an example of this in the ONVIF-AUDIT project here on github. ONVIF-AUDIT Project

You need to make a HTTP request with Digest Authentication.

RogerHardiman commented 3 years ago

I've cut this down from the original source, so mayhave introdced a typo. But you'll get the idea.

                            cam_obj.getSnapshotUri({ profileToken: videoSource.profileToken }, function (err, getUri_result) {

                                if (!err && getUri_result) {

                                    const fs = require('fs');
                                    const url = require('url');

                                    let filename = ""pic.jpg";
                                    let uri = url.parse(getUri_result.uri);

                                    let digestRequest = require('request-digest')(username, password);
                                    digestRequest.request({
                                        host: 'http://' + uri.host,
                                        path: uri.path,
                                        port: uri.port,
                                        encoding: null, // return data as a Buffer()
                                        method: 'GET'
                                    }, function (error, response, body) {
                                        if (error) {
                                            // console.log('Error downloading snapshot');
                                            // throw error;
                                        } else {

                                            fs.open(filename, 'w', function (err) {
                                                // callback for file opened, or file open error
                                                if (err) {
                                                    console.log('ERROR - cannot create output log file');
                                                }
                                                fs.appendFile(filename, body, function (err) {
                                                    if (err) {
                                                        console.log('Error writing to file');
                                                    }
                                                });

                                            });
                                        }
                                    });