繁体   English   中英

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

[英]I cannot connect to Splunk with the Javascript but can with Java

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

跨源政策无疑是CORS

在开始使用SDK进入更高级的用例时,绝对要注意跨域策略,但是在查看示例代码时,看起来好像在实例化service对象时无意间在变量名两边加上了双引号。

我复制了代码,将变量值替换到服务器上,第二次删除了双引号,并使用node在命令行中对其进行了验证……效果很好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM