Created
May 7, 2018 03:12
-
-
Save david-pm/b878d342e6bbdf987061f2b772743818 to your computer and use it in GitHub Desktop.
Binance REST API - signed request in Ruby
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
require 'net/http' | |
require 'openssl' | |
# https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md#general-api-information | |
# example curl request: | |
# `curl -H "X-MBX-APIKEY: #{api_key}" -X GET "https://api.binance.com/api/v3/account?recvWindow=100000×tamp=#{current_time}&signature=#{signature}"` | |
api_key = '<replace this with your api_key generated on binance>' | |
secret_key = '<replace this with your secret_key generated on binance>' | |
request_uri = 'https://api.binance.com/api/v3/account' | |
current_time = Time.now.to_i * 1000 | |
query_string = "recvWindow=5000×tamp=#{current_time}" | |
signature = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), secret_key, query_string) | |
params = { recvWindow: 5000, timestamp: current_time, signature: signature } | |
uri = URI.parse request_uri | |
uri.query = URI.encode_www_form params | |
http = Net::HTTP.new(uri.host, uri.port) | |
http.use_ssl = true | |
request = Net::HTTP::Get.new uri | |
reqquest.add_field('X-MBX-APIKEY', api_key) | |
response = http.request(request) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment