简体   繁体   English

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

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

I am trying to connect to Splunk with the Javascript. 我正在尝试使用Javascript连接到Splunk。 I am already connecting with the Java am able to do anything that I want. 我已经与Java连接,能够执行我想要的任何事情。 When I try to connect with Javascript I keep getting a 401. I am using the same credentials for both Java and Javascript, so I know there is no issue there. 当我尝试使用Javascript进行连接时,我一直收到401。我对Java和Javascript使用相同的凭据,因此我知道那里没有问题。 My code is directly out of the examples. 我的代码直接来自示例。 Here it is: 这里是:

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() {});
}

Here is the error message: 这是错误消息:

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 }

I run this on the command line with Node and get the 401 error. 我使用Node在命令行上运行此命令并得到401错误。 What else do I need to check to see what I am doing wrong. 我还需要检查什么才能查看我做错了什么。

跨源政策无疑是CORS

Cross-Origin policy is definitely something you want to look out as you start getting into more advanced use cases with the SDK but looking at your sample code, it looks like you unintentionally put double quotes around the variable names when instantiating the service object. 在开始使用SDK进入更高级的用例时,绝对要注意跨域策略,但是在查看示例代码时,看起来好像在实例化service对象时无意间在变量名两边加上了双引号。

I copied your code, replaced the variable values to my server, removed the double quotes second time around and verified it through command line using node ... it worked just fine. 我复制了代码,将变量值替换到服务器上,第二次删除了双引号,并使用node在命令行中对其进行了验证……效果很好。

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

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