Created
December 22, 2014 09:11
-
-
Save walm/cff80f2cbc32a7234af8 to your computer and use it in GitHub Desktop.
RedBridge API request sign
This file contains hidden or 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
# port from https://portal.redbridge.se/faq/api/guide/ | |
require 'openssl' | |
require 'digest/hmac' | |
require 'base64' | |
require 'cgi' | |
BASE_URL = "https://api.rbcloud.net/client/api" | |
API_KEY = "your api key" | |
SECRET_KEY = "your secret key" | |
# build the request API docs at http://docs.rbcloud.net/api/v1/TOC_User.html | |
request = { | |
command: "listVirtualMachines", | |
apikey: API_KEY, | |
response: "json" | |
} | |
def sign(str) | |
digest = OpenSSL::Digest.new('sha1') | |
hmac = OpenSSL::HMAC.digest(digest, SECRET_KEY, str) | |
Base64.encode64(hmac).strip | |
end | |
req_str = request.keys.sort.map { |key| "#{key}=#{CGI.escape request[key]}" }.join "&" | |
signatur = sign req_str.gsub(/\+/, "%20").downcase | |
# output full url | |
puts "#{BASE_URL}?#{req_str}&signature=#{CGI.escape signatur}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment