Created
July 13, 2018 18:26
-
-
Save nag-cheedella/2e454c91e4b543f291e6fd8d1c9a482f to your computer and use it in GitHub Desktop.
Ruby script to ingest thumbnails for Ooyala Video Assets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# Author : Ooyala | |
# Date : 13-Jul-2018 | |
# Purpose: Integrate with Ooyala API using signatures. | |
# This script uses Ooyala's algorithm for signing http requests documented under | |
# https://help.ooyala.com/video-platform/tasks/api_signing_requests.html | |
# Usage : ruby ooyala_api_call_with_signature.rb | |
# Pre-requisites: Ooyala Backlot account api_key, secret, asset embed_code, sample jpg file. | |
# install necessary gems listed in require section. | |
# Example demonstrated: Using Ooyala API to ingest preview image file | |
require 'rubygems' | |
require 'base64' | |
require 'cgi' | |
require 'digest/sha2' | |
require 'net/http' | |
require 'rest-client' | |
class OoyalaApi | |
DEFAULT_BASE_URL = 'http://api.ooyala.com' | |
DEFAULT_EXPIRATION_WINDOW = 10*60 # 10 mins | |
DEFAULT_API_KEY = '' #Ooyala api key | |
DEFAULT_SECRET_KEY = '' #Ooyala api secret | |
def generate_signature(http_method, request_path, query_params = {}, | |
request_body = '') | |
string_to_sign = DEFAULT_SECRET_KEY | |
string_to_sign += http_method.to_s.upcase + request_path | |
sorted_params = query_params.sort {|a, b| a[0].to_s <=> b[0].to_s} | |
string_to_sign += sorted_params.map {|param| param.join('=')}.join | |
string_to_sign += request_body.to_s | |
signature = Base64::encode64(Digest::SHA256.digest(string_to_sign))[0..42] | |
CGI.escape(signature) | |
end | |
def generate_api_call(http_method, request_path, query_params = {}, | |
request_body = nil, version_no = '/v2/') | |
http_method = http_method.to_s.upcase | |
request_path = request_path unless request_path[0..3] == version_no | |
original_params = Hash[*query_params.map {|k, v| [k.to_sym, v]}.flatten] | |
query_params = sanitize_and_add_needed_parameters(query_params) | |
query_params[:signature] ||= generate_signature(http_method, request_path, query_params.merge(original_params), request_body) | |
build_url(http_method, request_path, query_params) | |
end | |
def sanitize_and_add_needed_parameters(params) | |
params = Hash[*params.map {|k, v| [k.to_sym, CGI.escape(v.to_s)]}.flatten] | |
params[:expires] = Time.now.to_i + DEFAULT_EXPIRATION_WINDOW | |
params[:api_key] ||= DEFAULT_API_KEY | |
params | |
end | |
def build_url(http_method, request_path, query_params = {}) | |
url = DEFAULT_BASE_URL | |
url += request_path + '?' | |
url + query_params.sort {|a, b| a[0].to_s <=> b[0].to_s}.map do |param| | |
param.join("=") | |
end.join("&") | |
end | |
end | |
#initializing class | |
ooyala_api = OoyalaApi.new | |
params = {} | |
image_file_name = "./sample.jpg" # Path to the image file | |
request_body = File.open(image_file_name, "rb").read #Read file as binary | |
embed_code = "embed123" # Enter Ooyala video asset embed code | |
request_path = "/v2/assets/#{embed_code}/preview_image_files" #API for uploading preview image files | |
request_method = "POST" #SUPPORTED_HTTP_METHODS = %w{GET POST PUT PATCH DELETE} | |
url_with_signature = ooyala_api.generate_api_call(request_method, request_path, params, request_body, "/v2/") | |
puts "\n" + "Calling URL with signature:" + "\n" + url_with_signature.to_s + "\n" | |
request = RestClient::Request.new( | |
:method => request_method.to_s.downcase.to_sym, | |
:url => url_with_signature, | |
:payload => request_body) | |
response = request.execute | |
p "HTTP response code: #{response.code}" | |
p "Script Execution Completed" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment