2016-09-10 74 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特定的字典键,它的价值包含了我的额外参数,如价格,金额或地址。 –

相关问题