【问题标题】:Microsoft Azure Media Service. Unable to get token (app-only) by using REST API微软 Azure 媒体服务。无法使用 REST API 获取令牌(仅限应用程序)
【发布时间】:2017-08-14 12:53:14
【问题描述】:

我正在构建简单的 Angular2 应用程序,给定用户可以通过 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),那么有没有其他简单的方法来获取仅限应用程序的令牌?

【问题讨论】:

  • 由于 CORS,大多数现代浏览器都会阻止这种类型的调用。您将需要构建一个中间层 API,您的 AJAX 可以调用并通过 API 上的 CORS 设置信任您的 Web 应用程序。建议您考虑使用 Azure API 管理或 Azure Functions 作为自定义逻辑的主机。这也是保护用于调用媒体服务应用程序的服务主体凭据的好主意。
  • 谢谢@johndeu 我想这将是最快的解决方案。

标签: azure azure-media-services azure-api-apps


【解决方案1】:

我创建了返回令牌的 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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-15
    • 1970-01-01
    • 2015-11-18
    • 2016-10-27
    相关资源
    最近更新 更多