0

我正在尝试使用 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 错误。我还需要检查什么以查看我做错了什么。

4

2 回答 2

0

跨域政策无疑是CORS

于 2012-11-20T22:41:54.357 回答
0

当您开始使用 SDK 进入更高级的用例时,跨源策略绝对是您需要注意的事情,但是查看您的示例代码,您似乎在实例化service对象时无意中在变量名周围加上了双引号。

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

于 2012-11-20T23:47:46.123 回答