2016-09-10 25 views
0

我想使用Bitstamp API,並且能夠成功調用任何只需要密鑰,簽名和隨機數參數的任何事物。但是,當我嘗試轉移或訂購時,需要額外的參數(如地址,價格和金額),我的請求似乎變得混亂。我對編程,API和請求很陌生。如何在使用python請求模塊的bitstamp API中發送參數

def sell(self, product): 
    nonce = self.get_nonce() #an integer time.time()*10000 
    btc = self.get_btc_bal() #float 
    price = self.get_btcusd_bid() #float 
    amount = float(str(btc)[:5]) 
    message = str(nonce) + self.customer_id + self.api_key 
    signature = hmac.new(self.api_secret, msg=message, digestmod=hashlib.sha256).hexdigest().upper() 
    r = requests.post(self.url + 'sell/btcusd/', params={'key':self.api_key, 'signature':signature, 'nonce': nonce, 'amount': amount, 'price':price}) 
    r = r.json() 
    print(r) 
    print('open sell order in Bitstamp for %s BTC at %s USD'%(amount,price)) 

我確切的問題是如何正確地格式化/組織/編碼參數。當我像這樣把它,它返回

{"status": "error", "reason": "Missing key, signature and nonce parameters.", "code": "API0000"} 

如果我不使用params=返回

{"status": "error", "reason": "Invalid nonce", "code": "API0004"} 

我不相信,因爲我用的是完全一樣的get_nonce的現時原因()方法爲我所有的要求。我希望有人能看到我錯了,請和謝謝

回答

0

您應該使用數據=PARAMS

requests.post(self.url + 'sell/btcusd/', data={'key':self.api_key, 'signature':signature, 'nonce': nonce, 'amount': amount, 'price':price}) 

當您使用數據=,數據在發送請求的身體:

In [17]: req = requests.post("https://httpbin.org/post", data=data) 

In [18]: req.request.body 
Out[18]: 'foo=bar' 

In [19]: req.json() 
Out[19]: 
{u'args': {}, 
u'data': u'', 
u'files': {}, 
u'form': {u'foo': u'bar'}, 
u'headers': {u'Accept': u'*/*', 
    u'Accept-Encoding': u'gzip, deflate', 
    u'Content-Length': u'7', 
    u'Content-Type': u'application/x-www-form-urlencoded', 
    u'Host': u'httpbin.org', 
    u'User-Agent': u'python-requests/2.10.0'}, 
u'json': None, 
u'origin': u'178.167.254.183', 
u'url': u'https://httpbin.org/post'} 

使用PARAMS創建一個查詢字符串鍵和值對中的網址和請求沒有正文:

In [21]: req = requests.post("https://httpbin.org/post", params=data) 

In [22]: req.request.body 

In [23]: req.json() 
Out[23]: 
{u'args': {u'foo': u'bar'}, 
u'data': u'', 
u'files': {}, 
u'form': {}, 
u'headers': {u'Accept': u'*/*', 
    u'Accept-Encoding': u'gzip, deflate', 
    u'Content-Length': u'0', 
    u'Host': u'httpbin.org', 
    u'User-Agent': u'python-requests/2.10.0'}, 
u'json': None, 
u'origin': u'178.167.254.183', 
u'url': u'https://httpbin.org/post?foo=bar'} 

In [24]: req.url 
Out[24]: u'https://httpbin.org/post?foo=bar' 
+0

感謝您的幫助,我不知道。我試着用data =替換params =,它仍然返回相同的錯誤。我也嘗試了一些作爲參數和其他數據。我開始認爲這是一個API特定的字典鍵,它的價值包含了我的額外參數,如價格,金額或地址。 –

相關問題