Creating Private Videos

Hi @Seth

Sorry to hear you’re having trouble with private videos.

There is a fairly simple way to use the service workers to add the header. If you use a code similar to the one I shared in the jsfiddle in my previous answer, the ?avh parameter is added to the manifest url, even on IOS.

You can get the value of this parameter in your service worker and add it as a header to all subsequent requests.

Here is a service worker code sample that does this:

let sessionToken = {};

self.addEventListener('activate', (event) => {
   event.waitUntil(self.clients.claim());
});

self.addEventListener('fetch', function (event) {

   // if we're not on IOS, exit
   if (["iPad", "iPhone"].indexOf(event.target.navigator.platform) === -1) {
      return;
   }

   const iframeId = event.clientId || "-";

   // if requesting player.json, clear the value of the x-token-session
   if (event.request.url.indexOf("player.json") !== -1) {
      sessionToken[iframeId] = undefined;
   }

   // if we don't yet know the x-token-session value, try to retrieve it from the avh query param
   if (!sessionToken[iframeId]) {
      const re = /\?avh=([A-Za-z0-9-]+)/gm
      const reExec = re.exec(event.request.url);
      if (!!reExec) {
         sessionToken[iframeId] = reExec[1];
      }
   }

   if (!!sessionToken[iframeId] &&
      (event.request.url.startsWith("https://live.api.video") ||
         event.request.url.startsWith("https://cdn.api.video"))
   ) {
      event.respondWith(fetch(event.request, {
         headers: {
            "X-Token-Session": sessionToken[iframeId]
         }
      }));
   }
});

Hope this helps,

Olivier