Ruby - Binance API:签名未正确签名
Ruby - Binance API: Signature is not signing correctly
我正在尝试连接到 Binance API。
https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md
我被拒绝访问。我怀疑问题可能不是 'signature'
如果有人有向 Binance 签署剩余请求的经验,请指出我出错的正确方向。
error {"code":-1022,"msg":"Signature for this request is not valid."}
def get_time
endpoint = "/api/v1/time"
uri = @url + endpoint
uri = URI(uri)
response = Net::HTTP.get(uri)
data = JSON.parse(response)
data["serverTime"]
end
def get_amount
query = URI.encode_www_form("timestamp"=> get_time)
signature = sig(query)
query = URI.encode_www_form("timestamp"=> get_time, "signature" => signature)
endpoint = "/api/v3/account"
uri = @url + endpoint + '?' + query
uri = URI(uri)
req = Net::HTTP::Get.new(uri)
req['X-MBX-APIKEY'] = @api_key
res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https') do |http|
http.request(req)
end
puts "Sig: #{signature}"
puts "www: #{uri}"
res.body
end
def sig(query)
digest = OpenSSL::Digest::SHA256.new
OpenSSL::HMAC.hexdigest(digest, @api_secret, query)
end
似乎您调用了 get_time
两次,所以这可能是您的问题,因为 signed request documentation 指示签名应包含您的所有查询参数和请求正文连接。当您第二次调用 get_time
时,时间戳已在您使用第一次时间戳创建签名后更改。
试试这个
def get_amount
timestamp = get_time
query = URI.encode_www_form("timestamp"=> timestamp)
signature = sig(query)
query = URI.encode_www_form("timestamp"=> timestamp, "signature" => signature)
endpoint = "/api/v3/account"
uri = @url + endpoint + '?' + query
uri = URI(uri)
req = Net::HTTP::Get.new(uri)
req['X-MBX-APIKEY'] = @api_key
res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https') do |http|
http.request(req)
end
puts "Sig: #{signature}"
puts "www: #{uri}"
res.body
end
附带说明一下,您的 get_time
方法可以是 1 行:
def get_time
(Time.now.to_f * 1000).to_i.to_s
end
我正在尝试连接到 Binance API。
https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md
我被拒绝访问。我怀疑问题可能不是 'signature'
如果有人有向 Binance 签署剩余请求的经验,请指出我出错的正确方向。
error {"code":-1022,"msg":"Signature for this request is not valid."}
def get_time
endpoint = "/api/v1/time"
uri = @url + endpoint
uri = URI(uri)
response = Net::HTTP.get(uri)
data = JSON.parse(response)
data["serverTime"]
end
def get_amount
query = URI.encode_www_form("timestamp"=> get_time)
signature = sig(query)
query = URI.encode_www_form("timestamp"=> get_time, "signature" => signature)
endpoint = "/api/v3/account"
uri = @url + endpoint + '?' + query
uri = URI(uri)
req = Net::HTTP::Get.new(uri)
req['X-MBX-APIKEY'] = @api_key
res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https') do |http|
http.request(req)
end
puts "Sig: #{signature}"
puts "www: #{uri}"
res.body
end
def sig(query)
digest = OpenSSL::Digest::SHA256.new
OpenSSL::HMAC.hexdigest(digest, @api_secret, query)
end
似乎您调用了 get_time
两次,所以这可能是您的问题,因为 signed request documentation 指示签名应包含您的所有查询参数和请求正文连接。当您第二次调用 get_time
时,时间戳已在您使用第一次时间戳创建签名后更改。
试试这个
def get_amount
timestamp = get_time
query = URI.encode_www_form("timestamp"=> timestamp)
signature = sig(query)
query = URI.encode_www_form("timestamp"=> timestamp, "signature" => signature)
endpoint = "/api/v3/account"
uri = @url + endpoint + '?' + query
uri = URI(uri)
req = Net::HTTP::Get.new(uri)
req['X-MBX-APIKEY'] = @api_key
res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https') do |http|
http.request(req)
end
puts "Sig: #{signature}"
puts "www: #{uri}"
res.body
end
附带说明一下,您的 get_time
方法可以是 1 行:
def get_time
(Time.now.to_f * 1000).to_i.to_s
end