MFP 8.0 API 在 POSTMAN 中工作,但在 AJAX 中不工作

MFP 8.0 API works in POSTMAN but not from AJAX

我能够成功地向 POSTMAN 呼叫: /mfp/api/az/v1/token 和 /mfpadmin/management-apis/2.0/runtimes/mfp/applications

我正在获取从 /mfp/api/az/v1/token 收到的不记名令牌,并将其添加到 /mfp/applications 的授权 header 中。

我收到了两者的 200 响应,并从每个 API 获得了预期的信息。

然后我选择从 POSTMAN 复制 ajax 代码,用于每个工作 API 调用:

  var getBasic = {
    "async": true,
    "crossDomain": true,
    "url": "https://..../mfp/api/az/v1/token",
    "method": "POST",
    "headers": {
      "authorization": "Basic YXBpYzptZnBhcGlj",
      "grant_type": "client_credentials",
      "cache-control": "no-cache",
      "postman-token": "05a672e5-6141-fd6f-82e2-b282d68dce35",
      "content-type": "application/x-www-form-urlencoded"
    },
    "data": {
      "grant_type": "client_credentials",
      "scope": "settings.read"
    }
  }

  $.ajax(getBasic).done(function (response) {
    console.log(response);
    var accessToken = response.access_token;
    console.log(accessToken);
    var settings = {
      "async": true,
      "crossDomain": true,
      "url": "https://....:8445/mfpadmin/management-apis/2.0/runtimes/mfp/applications",
      "method": "GET",
      "headers": {
        "authorization": "Bearer " + accessToken,
        "cache-control": "no-cache"
        }
      }
    console.log(settings);
    $.ajax(settings).done(function (response) {
      console.log("response: " + response.totalListSize);
    });

  });

但是,当我 运行 在我的 Web UI 中使用这个时,我从 /token 收到了 200 响应 但我从 /mfp/applications

收到 401(未经授权)

为什么这在邮递员中有效,但在 Web 中却无效 UI (Chrome)?

您正在使用的 mfpadmin 服务及其端点 (applications) 不需要 您尝试过的方式需要访问令牌获得它。它需要控制台的用户名和密码。因此,当您使用 Bearer access-token 时,它会失败并显示 401 unauthorized,因为这不是服务器允许访问 applications 端点所期望的。

我做了以下事情:

  1. 安装了 expressrequest 节点包以创建各种代理。这是必需的,因为您不能简单地从浏览器向服务器发出 AJAX 请求(您会从浏览器收到与跨源请求相关的错误):

    npm init
    npm install --save express
    npm install --save request
    

    创建了一个proxy.js(注意此代码特定于mfpadmin):

    var express = require('express');
    var http = require('http');
    var request = require('request');
    
    var app = express();
    var server = http.createServer(app);
    var mfpServer = "http://localhost:9080";
    var port = 9081;
    
    server.listen(port);
    app.use('/', express.static(__dirname + '/'));
    console.log('::: server.js ::: Listening on port ' + port);
    
    // Reverse proxy, pipes the requests to/from MobileFirst Server
    app.use('/mfpadmin/*', function(req, res) {
         var url = mfpServer + req.originalUrl;
         console.log('::: server.js ::: Passing request to URL: ' + url);
         req.pipe(request[req.method.toLowerCase()](url)).pipe(res);
    });
    
  2. 在 HTML 文件中引用一个实现 .js 文件和 jQuery:

    <html>
        <head>
            <script src="/jquery-3.1.1.min.js"></script>
            <script src="/main.js"></script>
        </head>
    
        <body>
    
        </body>
    </html>
    
  3. 在 main.js 文件中:

    $.ajax({
       "crossDomain": true,
       "url": "http://localhost:9081/mfpadmin/management-apis/2.0/runtimes/mfp/applications",
       "method": "GET",
       "headers": {
           "authorization": "Basic YWRtaW46YWRtaW4=",
           "Access-Control-Allow-Origin": "*",
           "cache-control": "no-cache" 
       }      
    }).done(function(response) {
        console.log(response);
    });
    

    Basic YWRtaW46YWRtaW4=Basic Auth 的表示,用户名 admin 密码 admin

作为回复,我收到了以下 JSON。
items 数组包含当前在 MobileFirst Server 中注册的应用程序。