MFP 8.0 API适用于POSTMAN,但不适用于AJAX

时间:2017-02-08 15:19:44

标签: ajax api ibm-mobilefirst postman mobilefirst-server

我能够成功拨打POSTMAN: / mfp / api / az / v1 / token和/mfpadmin/management-apis/2.0/runtimes/mfp/applications

我正在接收从/ mfp / api / az / v1 / token收到的持有者令牌,并将其添加到/ mfp / applications的Authorization标头中。

我从两者收到200回复​​,并从每个API获取预期信息。

然后,我选择从POSTMAN复制这些工作API调用的ajax代码:

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

  });

然而,当我在我的WebUI中运行它时,我从/ token获得了200响应 但我从我的/ mfp / applications

获得了401(未经授权)

为什么这适用于邮递员,而不是来自网络用户界面(Chrome)?

1 个答案:

答案 0 :(得分:0)

您正在使用的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

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

    enter image description here