2017-02-08 55 views
0

我能够将POSTMAN调用成功: /mfp/api/az/v1/token和/mfpadmin/management-apis/2.0/runtimes/mfp/applicationsMFP 8.0 API在POSTMAN中工作,但不能从AJAX中工作

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

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

那么,我选择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); 
    }); 

    }); 

然而,当我在WebUI中运行这个我从/令牌 但我200响应从我的/ mfp /应用程序获得401(未授权)

为什么在邮递员中工作,但不是从Web UI(Chrome)工作?

+0

我想你应该获得由代码访问令牌,而不是简单地重新使用现有的令牌。你有没有尝试通过代码获得它? https://mobilefirstplatform.ibmcloud.com/tutorials/en/foundation/8。0/authentication-and-security/confidential-clients /#获取访问令牌 –

+0

我使用getBasic详细信息获取不记名令牌。当这个调用完成时,我从响应中获取access_token并将其传递给设置变量(/ mfp/applications)。 –

+0

你是什么意思的“我然后选择从邮递员复制ajax代码”? –

回答

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表示。

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

enter image description here

+0

是否需要在我们的服务器上设置node.js代理以使其正常工作? –

相关问题