Token Authentication
You can generate unique secure tokens for every video to enable authenticated video plays. A tokenized URL provides temporary access to your HLS video. Here is what it looks like
https://api.dyntube.com/v1/live/videos/tokens/TKMfds6D2ImFVA.m3u8?token=oXPAQ0TixktGi8WEKKJSDHJ&expires=1656597538
Please follow the following steps to generate tokenized video URLs.
Get the security key for token authentication from the dashboard. Below is the path in the dashboard:
Projects => Global Settings => API Keys.
Get the videos using the API and save the videos in your database for future reference. You will need the
key
field of the video element provided through the video API. The field key
is being referred as thevideoKey
in this article.The token represents the Base64 encoded SHA256 hash, based on the provided inputs. The token generation requires three required inputs and some optional parameters.
- 1.securityKey (required)
- 2.
- 3.videoKey (required)
- 4.countries (optional, comma-separated TWO letter country codes). Only viewers from provided countries will be allowed to watch the videos. If this parameter is empty, the video will play in all of the countries.
- 5.ips(optional, comma separated IP addresses).
If IP addresses are not provided then all IPs would be allowed.
- 6.views (optional). The total number of allowed video views.
The video views are counted against the Unix timestamp. So, if a Unix timestamp has a limit of 10 videos and 10 different people view a video with the same URL, the view counts for that video will be finished.
const express = require('express')
const app = express()
const port = 3000
global.crypto = require('crypto')
app.get('/', (req, res) => {
const securityKey="[API_SECURITY_KEY]";
const now = new Date();
now.setDate(now.getDate() + 1);
const expiryTime=Math.floor(now);
const videoKey="[VIDEO_KEY]";
const countries="",ips="";
const views=0;
var tokenInput = securityKey + expiryTime + countries + ips + videoKey;
tokenInput = views > 0 ? tokenInput + views.toString() : tokenInput;
var token = Buffer.from(crypto.createHash("sha256").update(tokenInput).digest()).toString("base64");
token = token.replace(/\n/g, '').replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_');
res.send(`https://api.dyntube.com/v1/live/videos/tokens/${videoKey}.m3u8?token=${token}&expires=${expiryTime}`
+ (countries ? `&countries=${countries}` : '')+ (ips ? `&ips=${ips}` : '')+ (views> 0 ? `&views=${views}` : ''));
})
app.listen(port, () => {
console.log(`app listening on port localhost:${port}`)
})
using System;
using System.Security.Cryptography;
using System.Text;
class Program
{
static void Main(string[] args)
{
string securityKey = "your_security_key_here";
long unixTimeMilliseconds = DateTimeOffset.Now.ToUnixTimeMilliseconds();
string videoKey = "your_video_key_here";
string countries = "US,CA";
string ips = "192.168.1.1,192.168.1.2";
long views = 5;
string token = GenerateToken(securityKey, unixTimeMilliseconds, videoKey, countries, ips, views);
Console.WriteLine(token);
}
private static string GenerateToken(string securityKey, long unixTimeMilliseconds, string videoKey, string countries = "", string ips = "", long views = 0)
{
var tokenInput = securityKey + unixTimeMilliseconds + countries + ips + videoKey;
tokenInput = views > 0 ? tokenInput + views : tokenInput;
var bytes = Encoding.UTF8.GetBytes(tokenInput);
var hash = new SHA256Managed().ComputeHash(bytes);
var token = Convert.ToBase64String(hash);
return token.Replace("\n", "").Replace("+", "-").Replace("/", "_").Replace("=", "");
}
}
import hashlib
import base64
import time
def generate_token(security_key: str, unix_time_milliseconds: int, video_key: str, countries: str = '', ips: str = '', views: int = 0) -> str:
token_input = security_key + str(unix_time_milliseconds) + countries + ips + video_key
token_input = token_input + str(views) if views > 0 else token_input
hash_value = hashlib.sha256(token_input.encode()).digest()
token = base64.b64encode(hash_value).decode().replace('\n', '').replace('+', '-').replace('/', '_').replace('=', '')
return token
function generateToken($securityKey, $unixTimeMilliseconds, $videoKey, $countries = "", $ips = "", $views = 0) {
$tokenInput = $securityKey . $unixTimeMilliseconds . $countries . $ips . $videoKey;
$tokenInput = $views > 0 ? $tokenInput . $views : $tokenInput;
$hash = hash("sha256", $tokenInput, true);
$token = base64_encode($hash);
$token = str_replace("\n", "", $token);
$token = str_replace("+", "-", $token);
$token = str_replace("/", "_", $token);
$token = str_replace("=", "", $token);
return $token;
}
c
You can call this function by passing in the required parameters, like this:
$securityKey = "your_security_key_here";
$unixTimeMilliseconds = time() * 1000; // current time in milliseconds
$videoKey = "your_video_key_here";
$countries = ""; // optional
$ips = ""; // optional
$views = 0; // optional
$token = generateToken($securityKey, $unixTimeMilliseconds, $videoKey, $countries, $ips, $views);
// Use the token to build the HLS URL
$hlsUrl = "https://api.dyntube.com/v1/live/videos/tokens/{$videoKey}.m3u8?token={$token}&expires={$unixTimeMilliseconds}&countries={$countries}&ips={$ips}&views={$views}";
- 1.You need to concatenate all parameters in the specified order.
var tokenInput = securityKey + expires + countries + ips + videoKey + viewId.
Thecountries
,ips and viewId
should be empty if you don't want to use them. - 2.
views
should be added to thetokenInput
only if greater than zero. - 3.Convert the above-concatenated string
tokenInput
into the byte array. - 4.Get the SHA256 hash of the byte array generated in the above step.
- 5.Convert hashed value to base 64 string.
- 6.Replace the following characters in the generated Base64 string: '\n' with '' (empty string), '+' with '-', '/' with '_' and '=' with '' (empty string).
Once you have generated the token, you can use this token to build a tokenized URL for your video.
Here is the format of the URL:
https://api.dyntube.com/v1/live/videos/tokens/[videoKey].m3u8?token=[TOKEN]&expires=[UNIXTIMESTAMP]&countries=[COUNTRIES]&ips=[IPS]&viewId=[VIEW_ID]&views=[NUMBER]
Here is an example:
https://api.dyntube.com/v1/live/videos/tokens/TKMfds6D2ImFVA.m3u8?token=oXPAQ0TixktGi8WEKKJSDHJ&expires=16597538&countries=AU,US&ips=1.1.1.1&viewId=abc&views=10
The URL without optional parameters will look like this:
https://api.dyntube.com/v1/live/videos/tokens/TKMfds6D2ImFVA.m3u8?token=oXPAQ0TixktGi8WEKKJSDHJ&expires=1656597538
Last modified 3mo ago