我无法使用Javascript连接到Splunk,但可以使用Java连接

时间:2012-11-20 22:29:58

标签: javascript splunk

我正在尝试使用Javascript连接到Splunk。我已经与Java连接,能够做任何我想做的事情。当我尝试连接Javascript时,我不断获得401.我使用相同的凭据同时使用Java和Javascript,所以我知道那里没有问题。我的代码直接来自示例。这是:

exports.main = function(opts, done) {
    // This is just for testing - ignore it
    opts = opts || {};

    var username = opts.username    || "username";
    var password = opts.password    || "password";
    var scheme   = opts.scheme      || "https";
    var host     = opts.host        || "domain.com";
    var port     = opts.port        || "8089";
    var version  = opts.version     || "5";

    var service = new splunkjs.Service({
        username: "username",
        password: "password",
        scheme: "https",
        host: "domain.com",
        port: "8089",
        version: "5"
    });

    // First, we log in
    service.login(function(err, success) {
        // We check for both errors in the connection as well
        // as if the login itself failed.
        if (err || !success) {
            console.log("Error in logging in");
            console.log(err);
            done(err || "Login failed");
            return;
        }

        // Now that we're logged in, let's get a listing of all the saved searches.
        service.savedSearches().fetch(function(err, searches) {
            if (err) {
                console.log("There was an error retrieving the list of saved searches:", err);
                done(err);
                return;
            }

            var searchList = searches.list();
            console.log("Saved searches:");
            for(var i = 0; i < searchList.length; i++) {
                var search = searchList[i];
                console.log("  Search " + i + ": " + search.name);
                console.log("    " + search.properties().search);
            }

            done();
        });
    });
};

if (module === require.main) {
    exports.main({}, function() {});
}

以下是错误消息:

There was an error retrieving the list of saved searches: { response: 
   { headers: 
      { connection: 'close',
        'content-length': '100',
        'content-type': 'text/xml; charset=utf-8',
        date: 'Tue, 20 Nov 2012 22:27:11 GMT',
        server: 'Splunkd' },
     statusCode: 401 },
  status: 401,
  data: '<response>\n<messages>\n<msg type="WARN">call not properly authenticated</msg>\n</messages>\n</response>',
  error: null }

我在Node的命令行上运行它并得到401错误。还有什么我需要检查,看看我做错了什么。

2 个答案:

答案 0 :(得分:0)

交叉原始政策毫无疑问CORS

答案 1 :(得分:0)

当您开始使用SDK进入更高级的用例但查看示例代码时,您希望了解Cross-Origin策略,看起来您在实例化{时无意中在变量名称周围添加了双引号{1}}对象。

我复制了你的代码,将变量值替换为我的服务器,第二次删除双引号,并通过命令行使用节点验证它...它运行得很好。