使用来自 Nodejs 应用程序的 OAuth2 令牌的 Ansible Tower API 调用

Ansible Tower API call using OAuth2 Token from Nodejs App

我们可以通过仅传递 Oauth2 令牌而不传递用户名或密码来调用 Ansible Tower Api 吗?

(假设我想通过仅将 Oauth2 令牌传递到我的 nodejs 脚本来从 ansible 塔获取我的库存。这可能吗?)

如果是,请分享该脚本的语法。

我使用的脚本给了我正确的输出,但它使用凭据作为用户名和密码,但我想通过仅传递 OAuth2 令牌(从我的用户名和密码生成)来完成相同的任务

var unirest = require('unirest');
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
var Request = unirest.get('http://<tower-host>/api/v2/inventories');

Request
   .auth({
    user: 'foo',
    pass: 'bar',
})
   .then(function (response) {
    var data = response.body;
    console.log(data);
  })

您可以简单地在 header 中传递授权令牌。 这是一个示例函数:

const fetch = require('node-fetch');
function job_inventory(tower_url, token) {
    return fetch(tower_url+ "/api/v2/inventories", {
        method: "GET",
        headers: {
            "content-type": "application/json",
            "authorization": "Bearer " + token
        }
    });
}