【问题标题】:MFP 8.0 API works in POSTMAN but not from AJAXMFP 8.0 API 在 POSTMAN 中有效,但在 AJAX 中无效
【发布时间】:2017-06-26 07:10:52
【问题描述】:

我能够成功地调用 POSTMAN 到: /mfp/api/az/v1/token 和 /mfpadmin/management-apis/2.0/runtimes/mfp/applications

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

我收到来自两者的 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(未授权)

为什么这在 postman 中有效,但在 Web UI (Chrome) 中无效?

【问题讨论】:

  • 我认为您应该通过代码获取访问令牌,而不是简单地重复使用现有令牌。您是否尝试通过代码获取它? mobilefirstplatform.ibmcloud.com/tutorials/en/foundation/8.0/…
  • 我正在使用 getBasic 详细信息获取不记名令牌。当该调用完成时,我从响应中获取 access_token 并将其传递给设置变量(/mfp/applications)。
  • “我然后选择从邮递员那里复制 ajax 代码”是什么意思?
  • Postman 有一个“代码”按钮,您可以选择让它输出您需要的呼叫类型。然后我选择一个 JS AJAX 调用。
  • 另请提及您尝试调用此代码的位置。在应用中还是在其他地方?

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


【解决方案1】:

您正在使用的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 中注册的应用程序。

【讨论】:

  • 是否需要在我们的服务器上设置 node.js 代理才能使其正常工作?
猜你喜欢
  • 2017-09-14
  • 1970-01-01
  • 2018-07-14
  • 2020-04-28
  • 2020-04-06
  • 1970-01-01
  • 2019-11-28
  • 2020-10-15
  • 2017-05-03
相关资源
最近更新 更多