Skip to content

Instantly share code, notes, and snippets.

Created October 22, 2019 15:34
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
require 'digest'
require 'openssl'
require 'base64'
require 'net/http'
require 'json'
require 'time'
TARGETS_PATH = '/targets'
def authorization_header_for_request(access_key, secret_key, method, content, content_type, date, request_path)
components_to_sign = []
components_to_sign << method
components_to_sign << Digest::MD5.hexdigest(content)
components_to_sign << content_type
components_to_sign << date
components_to_sign << request_path
string_to_sign = components_to_sign.join("\n")
signature = Base64.strict_encode64(OpenSSL::HMAC.digest('sha1', secret_key, string_to_sign))
"VWS #{access_key}:#{signature}"
def post_new_target(access_key, secret_key, name, image)
http_method = 'POST'
content_type = 'application/json'
image_base_64 = Base64.strict_encode64(, "rb").read).to_s
request_body = {width: 20, name: name, active_flag: 1, image: image_base_64}.to_json.to_s
date =
auth_header = authorization_header_for_request(access_key, secret_key, http_method, request_body, content_type, date, TARGETS_PATH)
http =, 443)
http.use_ssl = true
req =, 'Content-Type' => content_type, 'Accept' => content_type, 'Date' => date, 'Authorization' => auth_header)
req.body = request_body
res = http.request(req)
puts "response #{res.body}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment