首页 > 解决方案 > 使用 Javascript WinRT 将标头添加到 PlayReadyLicenseAcquisitionServiceRequest

问题描述

我很难弄清楚如何做一些我认为很简单的事情。在初始化视频流之前,DRM 服务器会为我们的应用程序提供一个令牌以在授权标头中使用。在我们的代码中,我们使用的是作为 PlayReady 一部分的 MediaProtectionManager。当需要许可调用时,管理器通过回调函数发送请求。据推测,我应该能够在等待结果之前向该请求添加一个标头,但我没有看到任何方法。该请求的类型为 PlayReadyLicenseAcquisitionServiceRequest。

const setupDRM = (onServiceRequested) => {
  const { MediaProtectionManager } = window.Windows.Media.Protection;
  const manager = new MediaProtectionManager();
  // other setup steps excluded for readability...
  manager.addEventListener('servicerequested', onServiceRequested, false);
};


const onServiceRequested = async (event) => {
  const { request } = event;
  appendToken(request); // Add authorization header with token
  const result = await request.beginServiceRequest();
};

const appendToken = (request) => {
   // How do I do this?
}

现在,我发现类似的问题得到了解答,但仅限于 C#。在所有这些情况下,答案是将请求克隆为 HTTP 请求,添加标头,然后等待结果。

public static async Task<bool> RequestLicenseManual(PlayReadyLicenseAcquisitionServiceRequest request, params KeyValuePair<string, object>[] headers)
{
  Debug.WriteLine("ProtectionManager PlayReady Manual License Request in progress");

  try
  {
    var r = request.GenerateManualEnablingChallenge();

    var content = new ByteArrayContent(r.GetMessageBody());

    foreach (var header in r.MessageHeaders.Where(x => x.Value != null))
    {
      if (header.Key.Equals("Content-Type", StringComparison.OrdinalIgnoreCase))
      {
        content.Headers.ContentType = MediaTypeHeaderValue.Parse(header.Value.ToString());
      }
      else
      {
        content.Headers.Add(header.Key, header.Value.ToString());
      }
    }

    var msg = new HttpRequestMessage(HttpMethod.Post, r.Uri) { Content = content };

    foreach (var header in headers)
    {
      msg.Headers.Add(header.Key, header.Value.ToString());
    }

    Debug.WriteLine("Requesting license from {0} with custom data {1}", msg.RequestUri, await msg.Content.ReadAsStringAsync());

    var client = new HttpClient();
    var response = await client.SendAsync(msg);

    if (response.IsSuccessStatusCode)
    {
      request.ProcessManualEnablingResponse(await response.Content.ReadAsByteArrayAsync());
    }
    else
    {
      Debug.WriteLine("ProtectionManager PlayReady License Request failed: " + await response.Content.ReadAsStringAsync());

      return false;
    }
  }
  catch (Exception ex)
  {
    Debug.WriteLine("ProtectionManager PlayReady License Request failed: " + ex.Message);

    return false;
  }

  Debug.WriteLine("ProtectionManager PlayReady License Request successfull");

  return true;
}

我已经能够在 JavaScript 中重现这一点,但似乎找不到将 getMessageBody 返回的字节数组转换为任何可用的方法,并且在 WinRt API 中找不到等效的 ByteArrayContent。无论如何,这一切似乎都是矫枉过正。难道不应该像在请求上调用 set header 函数一样简单吗?

标签: javascriptuwpwindows-runtime

解决方案


推荐阅读