Skip to content

Instantly share code, notes, and snippets.

@nag-cheedella
Last active September 19, 2017 16:59
Show Gist options
  • Save nag-cheedella/e780cce7cfa58899d67660be8510a8bb to your computer and use it in GitHub Desktop.
Save nag-cheedella/e780cce7cfa58899d67660be8510a8bb to your computer and use it in GitHub Desktop.
Ooyala API Signature generation example
//This gist is to generate signatures for making api calls to Ooyala Backlot APIs.
// This was prepared using the logic defined in http://help.ooyala.com/video-platform/tasks/api_signing_requests.html
// Alternatively, ooyala API SDK can be used. A sample ruby SDK is available here https://github.com/ooyala/ruby-v2-sdk
var c = test.openHttpClient();
//Insert the API key and Secret of your Ooyala account
var api_key="";
var secret = "";
//The end point for doing full CRUD
var baseURL = "https://api.ooyala.com/v2/assets/";
var expires = "";
var sig = "";
var asset_id ="";
//This field is to set a time out for test to fail before it receives api response from server.
var timeout = 12000;
test.log ("Begin Asset API CRUD calls");
test.beginTransaction();
//GET
test.beginStep("GET Assets",timeout);
test.log ("Begin Asset API GET call");
expires = Math.round((new Date().getTime()+300000)/1000);
sig = encodeURIComponent(utils.base64(utils.fromHex(utils.sha256(secret+"GET/v2/assets/api_key="+api_key+"expires="+expires))).substring(0,43));
var url = baseURL + "?api_key=" + api_key + "&signature=" + sig + "&expires=" + expires;
test.log("Asset API GET URL::: "+url);
var res = c.get(url,"items");
if (!res.isContentMatched()) {
test.log(res.getBody());
throw "Assets API GET request failed";
}
test.log ("End Asset API GET call");
test.endStep();
//POST
test.beginStep("Post a asset",timeout);
test.log ("Begin Asset API POST call");
expires = Math.round((new Date().getTime()+3000000)/1000);
name = "Test_Neustar_"+expires;
sig = encodeURIComponent(utils.base64(utils.fromHex(utils.sha256(secret+"POST/v2/assets/api_key="+api_key+"expires="+expires+"{\"name\":\""+name+"\",\"asset_type\":\"video\",\"file_name\":\"Neustar.mp4\",\"file_size\":\"1234\"}"))).substring(0,43));
var url = baseURL + "?api_key=" + api_key + "&signature=" + sig + "&expires=" + expires;
test.log("Asset API POST URL::: "+url);
var post = c.newPost(url);
schedule = JSON.stringify({
"name":name,
"asset_type":"video",
"file_name":"Sample.mp4",
"file_size":"1234"
});
post.setRequestBody(schedule, "application/json", "UTF-8");
var response = post.execute();
if (response.getStatusCode() != 200){
test.log("The response body:"+response.getBody());
throw "Assets API POST request failed.";
}
asset_id = JSON.parse(response.getBody()).embed_code;
test.log("Posted Asset ID::: "+asset_id);
test.log ("End Asset API POST call");
test.endStep();
//UPDATE
test.beginStep("Update an asset",timeout);
test.log ("Begin Asset API PATCH call");
expires = Math.round((new Date().getTime()+3000000)/1000);
test.log("Asset ID being used::: "+asset_id);
sig = encodeURIComponent(utils.base64(utils.fromHex(utils.sha256(secret+"PATCH/v2/assets/"+asset_id+"api_key="+api_key+"expires="+expires+"{\"description\":\"Neustar Testing\"}"))).substring(0,43));
var url = baseURL + asset_id + "?api_key=" + api_key + "&signature=" + sig + "&expires=" + expires;
test.log("Asset API PATCH URL::: "+url);
var patch = c.newPatch(url);
schedule = JSON.stringify({
"description":"Neustar Testing"
});
patch.setRequestBody(schedule, "application/json", "UTF-8");
var response = patch.execute();
if (response.getStatusCode() != 200){
test.log("The response body:"+response.getBody());
throw "Asset API PATCH request failed.";
}
test.log ("End Asset API PATCH call");
test.endStep();
//DELETE
test.beginStep("Delete an asset",timeout);
test.log ("Begin Asset API DELETE call");
expires = Math.round((new Date().getTime()+3000000)/1000);
test.log("Asset ID being used::: "+asset_id);
sig = encodeURIComponent(utils.base64(utils.fromHex(utils.sha256(secret+"DELETE/v2/assets/"+asset_id+"api_key="+api_key+"expires="+expires))).substring(0,43));
var url = baseURL + asset_id + "?api_key=" + api_key + "&signature=" + sig + "&expires=" + expires;
test.log("Asset API DELETE URL::: "+url);
var del = c.newDelete(url);
var response = del.execute();
if (response.getStatusCode() != 200){
test.log("The response body:"+response.getBody());
throw "Asset API DELETE request failed.";
}
test.log ("End Asset API DELETE call");
test.endStep();
test.endTransaction();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment