2017-02-08 97 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代理以使其正常工作? –

相關問題