2017-08-14 89 views
0

我正在構建簡單的Angular2應用程序,其中給定用戶可以通過Microsoft Azure Media Service REST API上傳視頻。Microsoft Azure媒體服務。無法通過使用REST API獲取令牌(僅限應用程序)

我試圖通過這一請求獲得令牌:

curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "grant_type=client_credentials&resource=https://rest.media.azure.net/&client_id=<application id>&client_secret=<password you selected for authentication>" https://login.microsoftonline.com/<Azure AD Tenant ID>/oauth2/token?api-version=2.11 

但它不會通過AJAX的工作(這是因爲CORS),那麼,有沒有其他簡單的方法來獲得一個應用程序,只令牌?

+0

大多數現代瀏覽器將阻止呼叫類型,由於CORS。您將需要構建一箇中間層API,您的AJAX可以調用該API,並通過API上的CORS設置來信任您的Web應用程序。建議您查看使用Azure API Management或Azure函數作爲自定義邏輯的主機。這對於保護用於調用媒體服務應用程序的服務主體憑據來說也是一個好主意。 – johndeu

+0

謝謝@johndeu我想這將是最快的解決方案。 – Alex91ckua

回答

0

我已經創建了PHP腳本,它返回令牌,我認爲這是最快的解決方案。

這裏是代碼,也許將是有用的人:

<?php 

define(APP_CLIENT_ID, '<client_id>'); 
define(CLIENT_SECRET, '<client_secret>'); 
define(AD_TENANT_ID, '<tenant_id>'); 
define(RESOURCE_URL, 'https://rest.media.azure.net'); 

function curlTokenRequest() { 

    $url = "https://login.microsoftonline.com/".AD_TENANT_ID."/oauth2/token"; 

    $request = "grant_type=client_credentials&resource=".RESOURCE_URL."&client_id=".APP_CLIENT_ID."&client_secret=".CLIENT_SECRET; 

    $ch = curl_init($url); 
    $options = array(
     CURLOPT_RETURNTRANSFER => true,   // return web page 
     CURLOPT_HEADER   => false,  // don't return headers 
     CURLOPT_FOLLOWLOCATION => false,   // follow redirects 
     // CURLOPT_ENCODING  => "utf-8",   // handle all encodings 
     CURLOPT_AUTOREFERER => true,   // set referer on redirect 
     CURLOPT_CONNECTTIMEOUT => 200,   // timeout on connect 
     CURLOPT_TIMEOUT  => 200,   // timeout on response 
     CURLOPT_POST   => 1,   // i am sending post data 
     CURLOPT_POSTFIELDS  => $request, // this are my post vars 
     CURLOPT_SSL_VERIFYHOST => 0,   // don't verify ssl 
     CURLOPT_SSL_VERIFYPEER => false,  // 
     CURLOPT_VERBOSE  => 1, 
     CURLOPT_HTTPHEADER  => array(
      "Content-Type: application/x-www-form-urlencoded" 
     ) 

    ); 

    curl_setopt_array($ch,$options); 
    $data = curl_exec($ch); 
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
    $curl_errno = curl_errno($ch); 
    $curl_error = curl_error($ch); 
    //echo $curl_errno; 
    //echo $curl_error; 
    curl_close($ch); 

    http_response_code($httpcode); 
    header('Content-Type: application/json'); 
    header('Access-Control-Allow-Origin: *'); 

    return $data; 
} 
相關問題