Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如果使用POST请求在vcenter中启动会话,如何获取身份验证令牌_Node.js_Request_Vcenter - Fatal编程技术网

Node.js 如果使用POST请求在vcenter中启动会话,如何获取身份验证令牌

Node.js 如果使用POST请求在vcenter中启动会话,如何获取身份验证令牌,node.js,request,vcenter,Node.js,Request,Vcenter,我正在尝试获取会话令牌以访问VCenter 我试着通过邮递员得到它,而邮递员的基本身份没有任何问题。当我尝试在NodeJS中执行相同操作时,会得到空数据 这是我的密码 async function getListPolicy(url, username, password) { var auth = "Basic " + new Buffer.from(username + ":" + password).toString("base64"); var header = {

我正在尝试获取会话令牌以访问VCenter

我试着通过邮递员得到它,而邮递员的基本身份没有任何问题。当我尝试在NodeJS中执行相同操作时,会得到空数据

这是我的密码

async function getListPolicy(url, username, password) {
    var auth = "Basic " + new Buffer.from(username + ":" + password).toString("base64");
    var header = {
        Authorization: auth,
        'Content-Type': 'application/json'
    }
    var options = {
        method: 'POST',
        url: url + '/rest/com/vmware/cis/session',
        headers: header,
    };
    return new Promise(function (resolve, reject) {
        request(options, function (error, response, body) {
            if (error) throw new Error(error);
            console.log(body);
            resolve(body);
        });
    });
}
在《邮递员》中,我只使用了
授权
基本授权
。它返回一个带有键的值,这就是我想要得到的

多谢各位

编辑: 在这里调用该文件

const express = require("express");
const router = express.Router();
const getPolicies = require('./getPolicies.js');

router.post("/getListPolicy", getListPolicy);

module.exports = router;


async function getListPolicy(req, res, next) {
    var url = "https://xxx/";
    var username = "administrator@vsphere.local";
    var password = "xxx";
    var token = await getPolicies.getListPolicy(url, username, password);
    res.json(token);
}

根据
request
api,对于
Basic
auth,您可以在选项中提供
auth
键。此外,还可以在url中指定

从:

或者


你能检查一下这些选项吗<代码>变量选项={method:'POST',url:url+'/rest/com/vmware/cis/session',headers:header,auth':{'user':'username','pass':'password',}从标题中删除身份验证已添加为答案。如果有错误,是否可以发布。控制台记录或目录错误。此外,如果出现错误,请执行拒绝<代码>如果(错误)拒绝(错误)没有任何。。。它什么也回答不了。只是一个空白的
[nodemon]由于更改而重新启动。。。[nodemon]正在端口8080上启动`node server.js`侦听*空白*等一分钟,您是在某处调用
getListPolicy
?某种模型还是远程方法?你在用遥控方法吗?准确地说,这不起作用。我没有解决任何问题。打印的只是标题^^ ^'
request.get('http://some.server.com/', {
  'auth': {
    'user': 'username',
    'pass': 'password',
    'sendImmediately': false
  }
});
var username = 'username',
    password = 'password',
    url = 'http://' + username + ':' + password + '@some.server.com';

request({url: url}, function (error, response, body) {
   // Do more stuff with 'body' here
});