Sample Signature Code (Ruby)

This following is Ruby sample code for generating signatures:

require "digest/sha2"
require "base64"

# Both of these values should be obtained from the Developers tab in Backlot.
API_KEY = ""
SECRET = ""

module OoyalaApi
  def self.generate_signature(secret, http_method, request_path, query_string_params, request_body)
    string_to_sign = secret + http_method + request_path
    sorted_query_string = query_string_params.sort { |pair1, pair2| pair1[0] <=> pair2[0] }
    string_to_sign += sorted_query_string.map { |key, value| "#{key}=#{value}"}.join
    string_to_sign += request_body.to_s
    signature = Base64::encode64(Digest::SHA256.digest(string_to_sign))[0..42].chomp("=")
    return signature
  end
end

# Example usage of the generate_signature function:
#
# Set `expires` in 1-hour intervals for higher caching performance:
# t = Time.now
# expires = Time.local(t.year, t.mon, t.day, t.hour + 1).to_i
#
# params = { "api_key" => API_KEY, "expires" => expires }
# signature = OoyalaApi.generate_signature(SECRET, "GET", "/v2/players/HbxJKM", params, nil)
   

Was this article helpful?