简体   繁体   中英

I cannot connect to Splunk with the Javascript but can with Java

I am trying to connect to Splunk with the Javascript. I am already connecting with the Java am able to do anything that I want. 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. 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. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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