2014-02-11 20 views
1

我試圖使用API​​訪問Bitstamp帳戶的餘額。Ruby中的Bitstamp API簽名

secret = "secret" 
key = "key" 
nonce = (1000*Time.now.to_f).to_i.to_s 
client_id = "123123" 

message = nonce + client_id + key 
signature = HMAC::SHA256.hexdigest(secret, message).upcase 

puts open("https://www.bitstamp.net/api/balance/?nonce=#{nonce}&key=#{key}&signature=#{signature}").read 

它清楚地生成所有需要的屬性

https://www.bitstamp.net/api/balance/?nonce=1392137355403&key=key&signature=955A3FFC6FEBE69385B9503307873DBCD21E9B7B8EDE67817FFF70961189CE50 

但錯誤說屬性丟失,爲什麼呢?

{"error": "Missing key, signature and nonce parameters"} 
+1

我快速查看了API並複製了您的錯誤。我認爲服務不是按預期工作,或者更可能是錯誤信息只是無益的。該文檔建議您使用POST,但不指示如何發送或格式化請求參數。我會建議Bitstamp開發人員需要更詳細的文檔,更好的錯誤消息或可能參考客戶端。 –

+0

就是這樣,我不得不以POST的形式發送請求。 – Sergey

+0

@Sergey - 你解決了上述問題嗎?我也被困在這裏嗎?該API沒有很好的記錄。如果你已經解決了這個問題,你可以寄給我你的代碼生成代碼和發佈請求代碼嗎? – Sam

回答

2

問題是我發送的請求是GET而不是POST。這裏是我現在使用的完整代碼。

require 'open-uri' 
require 'json' 
require 'base64' 
require 'openssl' 
require 'hmac-sha2' 
require 'net/http' 
require 'net/https' 
require 'uri' 

def bitstamp_private_request(method, attrs = {}) 
    secret = "xxx" 
    key = "xxx" 
    client_id = "xxx" 
    nonce = nonce_generator 

    message = nonce + client_id + key 
    signature = HMAC::SHA256.hexdigest(secret, message).upcase 

    url = URI.parse("https://www.bitstamp.net/api/#{method}/") 
    http = Net::HTTP.new(url.host, url.port) 
    http.use_ssl = true 

    data = { 
    nonce: nonce, 
    key: key, 
    signature: signature 
    } 
    data.merge!(attrs) 
    data = data.map { |k,v| "#{k}=#{v}"}.join('&') 

    headers = { 
    'Content-Type' => 'application/x-www-form-urlencoded' 
    } 

    resp = http.post(url.path, data, headers) 
    console_log "https://www.bitstamp.net/api/#{method}/" 
    resp.body 
end 

def nonce_generator 
    (Time.now.to_f*1000).to_i.to_s 
end