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.
Step 1. Get Security Key
Get the security key for token authentication from the dashboard. Below is the path in the dashboard:
Step 2. Get Video Key
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.
Step 3. Generate token
The token represents the Base64 encoded SHA256 hash, based on the provided inputs. The token generation requires three required inputs and some optional parameters.
securityKey (required)
expires (required) Unix timestamp, Token will expire after this time.
videoKey (required)
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.
ips(optional, comma separated IP addresses).
If IP addresses are not provided then all IPs would be allowed.
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.
Here is a code example in Node.js
Here is a code example in c#
Python sample
PHP Sample
You can call this function by passing in the required parameters, like this:
Steps To Generate a Token
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.views
should be added to thetokenInput
only if greater than zero.Convert the above-concatenated string
tokenInput
into the byte array.Get the SHA256 hash of the byte array generated in the above step.
Convert hashed value to base 64 string.
Replace the following characters in the generated Base64 string: '\n' with '' (empty string), '+' with '-', '/' with '_' and '=' with '' (empty string).
Step 4. Build a Tokenized Video URL
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 updated