Generating OTP for offline use
Offline video is available with our Android and iOS SDK. It is also available with React Native and Flutter SDK. You can allow your viewers to download an encrypted version of the video to their Android devices. This video can be made to expire after a certain time by specifying a rental duration. The access token in this case also need to specified to allow persistence of license.
This code below creates an OTP with permission to persist on viewer's device. The downloaded file will be valid for the 15 days from the time of setup.
{
"licenseRules": json_serialize({
"canPersist": true,
"rentalDuration": 15 * 24 * 3600,
})
}
It is also possible to specify the time from which the license can be valid AFTER the first time use.
Create offline OTPβ
The sample videoID is 1234567890
and the API Secret Key is a1b2c3d4e5
. The rental duration is set to 15 days
(1296000 seconds) in the following sample codes.
The sample code only passes the OTP licenseRules
as parameter. It is possible to combine this with other OTP
parameters
Note that the value of licenseRules
is a string with serialized JSON and not a JSON object
- CURL
- NODE
- PHP
- C#
curl -X POST \
https://dev.vdocipher.com/api/videos/1234567890/otp \
-H 'Accept: application/json' \
-H 'Authorization: Apisecret a1b2c3d4e5' \
-H 'Content-Type: application/json' \
-d '{
"licenseRules": "{\"canPersist\": true, \"rentalDuration\": 1296000}"
}'
var request = require('request');
var options = {
method: 'POST',
url: 'https://dev.vdocipher.com/api/videos/1234567890/otp',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Apisecret a1b2c3d4e5',
},
body: {
licenseRules: JSON.stringify({
canPersist: true,
rentalDuration: 15 * 24 * 3600,
}),
},
json: true,
};
request(options, function (error, response, body) {
if (error) throw new Error(error);
console.log(body);
});
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://dev.vdocipher.com/api/videos/1234567890/otp",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'licenseRules' => json_encode([
'canPersist' => true,
'rentalDuration' => 15 * 24 * 3600
])
]),
CURLOPT_HTTPHEADER => [
"Accept: application/json",
"Authorization: Apisecret a1b2c3d4e5",
"Content-Type: application/json"
],
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
var client = new RestClient("https://dev.vdocipher.com") {
// using user secrets to keep api key
Authenticator = new OAuth2AuthorizationRequestHeaderAuthenticator(_config["VdoApiKey"], "Apisecret")
};
var otpParameters = new
{
// 5 minutes until OTP is used
// only used when setting up offline, not again
ttl = 300,
licenseRules = JsonSerializer.Serialize(new
{
rentalDuration= 15 * 24 * 3600, // 15 days
canPersist = true,
}),
};
Debug.Assert(videoId?.Length == 32, "videoId is not valid");
var url = $"/api/videos/{videoId}/otp";
var request = new RestRequest(url).AddJsonBody(otpParameters);
var cancellationToken = CancellationToken.None;
var response = await client.ExecutePostAsync(request, cancellationToken);
if (response.Content == null) return Json(new {message= "Invalid API response"});
var otpObject = JsonSerializer.Deserialize<Dictionary<string, string>>(response.Content);
// otpObject contain {otp: string, playbackInfo: string}
return Json(otpObject);