Electron对接VAPIX接口遇401认证失败问题求助
VAPIX接口Electron项目认证失败问题求助
我正在开发对接VAPIX接口的Electron项目,用于发送PTZ移动指令。测试阶段先调用ping测试接口:https://192.168.30.10/axis-cgi/pingtest.cgi?ip=192.168.30.10。该接口在Chrome浏览器中,无论是通过弹窗输入用户名密码,还是使用https://user:pass@192.168.30.10/axis-cgi/pingtest.cgi?ip=192.168.30.10形式的URL,都能正常返回“got response”。但在Electron项目中,以下两种认证方式均失败:
基础认证尝试及报错
使用axios基础认证调用接口,返回401 Unauthorized错误,代码及报错信息如下:
const axios = require('axios'); const https = require('https'); // command is https://192.168.30.10/axis-cgi/pingtest.cgi?ip=192.168.30.10 async function sendTestCommand( event, command ) { const username = 'root'; const password = 'password'; const agent = new https.Agent({ rejectUnauthorized: false // This bypasses SSL certificate validation. }); const basicAuth = 'Basic ' + Buffer.from( username + ':' + password ).toString( 'base64' ); const config = { httpsAgent: agent, headers: { 'Authorization': basicAuth, } }; console.log( 'Headers:', config.headers ); try { const response = await axios.get( command, config ); console.log( 'Response:', response.data ); return response.data; } catch ( error ) { console.error( 'Error:', error.response ? error.response.data : error.message ); return { error: true, message: error.message }; } }
报错信息:
Error: <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>401 Unauthorized</title> </head><body> <h1>Unauthorized</h1> <p>This server could not verify that you are authorized to access the document requested. Either you supplied the wrong credentials (e.g., bad password), or your browser doesn't understand how to supply the credentials required.</p> </body></html>
摘要认证尝试及报错
改用axios-digest进行摘要认证,出现“Auth params error”错误,代码及报错信息如下:
const AxiosDigest = require('axios-digest').default; const https = require('https'); // command is https://192.168.30.10/axis-cgi/pingtest.cgi?ip={}} async function sendTestCommand(event, command) { const username = 'root'; const password = 'password'; const httpsAgent = new https.Agent({ rejectUnauthorized: false, }); const axiosDigest = new AxiosDigest(username, password); console.log(axiosDigest.info); axiosDigest.post( command, {}, {httpsAgent} ) .then(response => { console.log(response.data); }) .catch(error => { console.error('Request failed', error); }); }
报错信息:
{ username: 'root', passwd: '***' } Request failed Error: Auth params error. at AxiosDigest.getAuthHeader (C:\Users\Administrator\Documents\ElectronCameraOverlay\node_modules\axios-digest\index.js:177:19) at AxiosDigest.<anonymous> (C:\Users\Administrator\Documents\ElectronCameraOverlay\node_modules\axios-digest\index.js:89:34) at step (C:\Users\Administrator\Documents\ElectronCameraOverlay\node_modules\axios-digest\index.js:33:23) at Object.throw (C:\Users\Administrator\Documents\ElectronCameraOverlay\node_modules\axios-digest\index.js:14:53) at rejected (C:\Users\Administrator\Documents\ElectronCameraOverlay\node_modules\axios-digest\index.js:6:65) at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
请求帮忙解决该认证失败问题。
内容的提问来源于stack exchange,提问作者Ubernator
相关产品推荐
相关产品推荐

