【问题标题】:How to do request HTTP Digest auth with Node.JS?如何使用 Node.JS 请求 HTTP Digest 身份验证?
【发布时间】:2013-06-11 05:43:21
【问题描述】:

我必须使用 Node.JS 为 API 文档编写一些代码,但最近几天我尝试了我在网络上找到的所有解决方案(当然包括 Stack),但没有成功......

我的 API 使用 HTTP Digest Auth,这就是问题所在,我能够连接,这没什么大不了的,但每次我得到相同的回报:

Got response : 401
HTTP Digest Authentication required for "api.example.com"

您可以在下面显示我的基本代码而无需身份验证!因为在我做了所有尝试之后我不知道我能做什么:

var http = require('http')

var options = {
    host: 'api.example.com',
    path: '/example/1.xml',
};

var request = http.get(options, function(res){
    var body = "";
    res.on('data', function(data){
        body += data;
    })
    res.on('end', function(){
        console.log('Got response : ' + res.statusCode);
        console.log(body);
    })
    res.on('error', function(e){
        console.log('Got error : ' +e.message);
    });
});

我最后一次尝试是使用这个模块https://npmjs.org/package/request,但每次我得到 401 时他都不能正常工作!

有关更多信息,我能够使用 Ruby、Python、php 和 Java 从我的 API 连接并获取我需要的信息,因此我确信我的 API 运行良好并且我传递的信息是正确的。 我使用 Node v0.10.11 的最后一个稳定版!

如果有人可以帮助我或有最新的解决方案,我会很高兴。

编辑: 我将使用模块Mickael/request添加一些关于我的测试的详细信息

第一次尝试:

var request = require('request')

var options = {
    'url': 'http://api.example.fr/example/1.xml',
    'auth': {
        'user': 'test',
        'pass': 'test',
        'sendImmediately': false
    }
};

var request = request.get(options, function(error, response, body){
    if (!error && response.statusCode == 200){
        console.log('body : ' + body)
    }
    else{
        console.log('Code : ' + response.statusCode)
        console.log('error : ' + error)
        console.log('body : ' + body)
    }
});

第二次尝试:

var request = require('request')

request.get('http://api.example.fr/example/1.xml', function(error, response, body){
    if (!error && response.statusCode == 200){
        console.log('body : ' + body)
    }
    else{
        console.log('Code : ' + response.statusCode)
        console.log('error : ' + error)
        console.log('body : ' + body)
    }
}).auth('test', 'test', false);

但是返回还是一样的401

【问题讨论】:

    标签: node.js httprequest digest digest-authentication http-digest


    【解决方案1】:

    这是您的示例,已根据其 API 更正为使用 request

    var options = {
      uri: 'http://api.example.fr/example/1.xml',
      auth: {
        user: 'test',
        pass: 'test',
        sendImmediately: false
      }
    };
    request(options, function(error, response, body){
        if (!error && response.statusCode == 200){
            console.log('body : ' + body)
        }
        else{
            console.log('Code : ' + response.statusCode)
            console.log('error : ' + error)
            console.log('body : ' + body)
        }
    });
    

    请求链式 API 有点令人困惑(恕我直言),但我相信你也可以让它工作。

    【讨论】:

    • 感谢您的建议,但正如我在原始帖子中所说,我尝试了此解决方案,您可以在我的帖子末尾找到链接!
    • 使用request 发布您的代码。 “我试过了,但没用”不足以让我们真正为您提供帮助。
    • 我正在编辑以使用 request 添加我的代码。谢谢你的帮助:)
    • 您好,很抱歉延迟返回,但我试过了,但还是不行:/有关更多信息,我的 API 是 [Frapi]getfrapi.com
    • 有史以来最烦人的事情:说“它没用”。状态码是什么?编程是一项注重细节的工作。除非你开始注重细节,否则我帮不了你。我用“getfrapi.com/example/1.xml”运行了我的示例并得到了 404。您可以在命令行上使用curl 来请求工作吗?以curl -v --digest --user test:test http://getfrapi.com/example/1.xml 为例。
    【解决方案2】:

    请求包中的摘要认证似乎不完整。

    你可以试试:https://npmjs.org/package/http-digest-client,它是一个相当不错的摘要认证轻量级实现。

    如果您需要使用要发送的正文消息进行摘要身份验证 POST,您可以将 request 与 http-digest-client 结合使用。安装后,只需在 node-modules 下打开 http-digest-client 代码,并将其对 http 包的使用替换为 request 包 api。

    【讨论】:

      【解决方案3】:

      试试urllib,它将适用于简单和消化的身份验证。

      const httpClient = require('urllib');
      const url = 'https://site.domain.com/xmlapi/xmlapi';
      const options = {
        method: 'POST',
        rejectUnauthorized: false,
        // auth: "username:password" use it if you want simple auth
        digestAuth: "username:password",
        content: "Hello world. Data can be json or xml.",
        headers: {
           //'Content-Type': 'application/xml'  use it if payload is xml
           //'Content-Type': 'application/json' use it if payload is json 
          'Content-Type': 'application/text'
        }
      };
      const responseHandler = (err, data, res) => {
        if (err) {
          console.log(err);
        }
        console.log(res.statusCode);
        console.log(res.headers);
        console.log(data.toString('utf8'));
      }
      httpClient.request(url, options, responseHandler);
      

      【讨论】:

        【解决方案4】:

        你的样本是有效的

        var request = require('request')
        
        request.get('http://api.example.fr/example/1.xml', function(error, response, body){
            if (!error && response.statusCode == 200){
                console.log('body : ' + body)
           }
            else{
               console.log('Code : ' + response.statusCode)
               console.log('error : ' + error)
               console.log('body : ' + body)
           }
        }).auth('test', 'test', false);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2023-03-10
          • 1970-01-01
          • 2020-09-04
          • 2012-06-25
          • 1970-01-01
          • 1970-01-01
          • 2011-12-05
          相关资源
          最近更新 更多