2017-02-08 10 views
0

/mfp/api/az/v1/tokenと/mfpadmin/management-apis/2.0/runtimes/mfp/applicationsへのPOSTMANの呼び出しを成功させることができました。MFP 8.0 APIはPOSTMANで動作しますが、AJAXでは動作しません

私は/ mfp/api/az/v1/tokenから受け取ったベアラトークンを取得し、それを/ mfp/applicationsのAuthorizationヘッダーに追加します。

私は両方から200応答を受け取り、各APIから期待される情報を取得します。

私は、これらの作業のAPI呼び出しのそれぞれについて、POSTMANから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でこれを実行すると、私は /トークンから200応答を取得しますが、私

これは郵便受けでは動作しますが、Web UI(Chrome)では動作しないのはなぜですか? mfpadminサービスと、使用しているそのエンドポイント(applications

+0

は、私はあなたのコードでアクセストークンを取得することになって、単に既存のトークンを再利用していないと思います。コードで取得しようとしましたか? https://mobilefirstplatform.ibmcloud.com/tutorials/ja/foundation/8。0/authentication-and-security/confidential-clients /#アクセストークン取得 –

+0

getBasicの詳細を使用してベアラトークンを取得しています。その呼び出しが完了すると、私はレスポンスからaccess_tokenを取得し、それを設定変数(/ mfp/applications)に渡します。 –

+0

「郵便配達員からのajaxコードのコピーを選択する」とはどういう意味ですか? –

答えて

0

ないは、あなたがそれを得ることを試みたように、アクセストークンを必要としません。コンソールにはユーザー名とパスワードが必要です。したがって、Bearer access-tokenを使用している場合は、で失敗します。これは、サーバーがapplicationsエンドポイントへのアクセスを許可するために期待しているものではないためです。

私は次のことを行っている:

  1. は一種のプロキシを作成するためにexpressrequestノードパッケージをインストール。あなたは、単にブラウザからサーバにAJAX要求を行うことができませんので、これは(あなたが原点リクエストを横断する関連ブラウザからエラーが発生します)が必要です:

    proxy.js(ノートこのように作成
    npm init 
    npm install --save express 
    npm install --save request 
    

    コード)mfpadminに特異的である:HTMLファイル参照で

    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. 実装.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=は、ユーザー名とパスワードadminadminBasic Authの表現です。

応答として、私は以下のJSONを受け取りました。
itemsアレイには、MobileFirst Serverに現在登録されているアプリケーションが含まれています。

enter image description here

+0

node.jsプロキシをサーバーにも設定する必要がありますか? –

関連する問題