Skip to content

Instantly share code, notes, and snippets.

@vlastachu
Created October 22, 2019 15:34
Show Gist options
  • Save vlastachu/be1afbef582dc4943b562889ac1e26d3 to your computer and use it in GitHub Desktop.
Save vlastachu/be1afbef582dc4943b562889ac1e26d3 to your computer and use it in GitHub Desktop.
require 'digest'
require 'openssl'
require 'base64'
require 'net/http'
require 'json'
require 'time'
VWS_ENDPOINT = 'vws.vuforia.com'
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}"
end
def post_new_target(access_key, secret_key, name, image)
http_method = 'POST'
content_type = 'application/json'
image_base_64 = Base64.strict_encode64(File.open(image, "rb").read).to_s
request_body = {width: 20, name: name, active_flag: 1, image: image_base_64}.to_json.to_s
date = Time.now.httpdate.to_s
auth_header = authorization_header_for_request(access_key, secret_key, http_method, request_body, content_type, date, TARGETS_PATH)
http = Net::HTTP.new(VWS_ENDPOINT, 443)
http.use_ssl = true
req = Net::HTTP::Post.new(TARGETS_PATH, 'Content-Type' => content_type, 'Accept' => content_type, 'Date' => date, 'Authorization' => auth_header)
req.body = request_body
res = http.request(req)
puts "response #{res.body}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment