我正在尝试连接到Binance API。
https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md
我被拒绝访问。我怀疑问题可能不是“签名”
如果有人有向Binance签署休息请求的经验,那么朝正确的方向指出我要去哪里的错误将是值得的。
错误{“代码”:-1022,“信息”:“此请求的签名无效。”}
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
答案 0 :(得分:1)
好像您要两次调用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