【问题标题】:How to send parameters in bitstamp api using python requests module如何使用python请求模块在bitstamp api中发送参数
【发布时间】:2018-11-18 18:54:35
【问题描述】:

我正在尝试使用 Bitstamp api,并且能够成功调用任何只需要密钥、签名和 nonce 参数的东西。但是,当我尝试转移或订购时,需要额外的参数,如地址或价格和金额,我的请求似乎搞砸了。我是编程、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"}

我不相信 nonce 的原因,因为我对所有请求都使用完全相同的 get_nonce() 方法。我希望有人能看到我错在哪里,谢谢

【问题讨论】:

    标签: python python-requests bitcoin


    【解决方案1】:

    您应该使用 data = 而不是 params

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

    当你使用data =时,数据在请求体中发送:

    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 在 url 中创建一个带有键/值对的查询字符串,并且请求没有正文:

    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'
    

    【讨论】:

    • 谢谢你的帮助,我不知道。我尝试用 data= 替换 params= ,它仍然返回相同的错误。我也尝试了一些作为参数和其他作为数据。我开始认为这是一个特定于 api 的字典键,它的值包含我的额外参数,如价格、金额或地址。
    【解决方案2】:

    关于你得到的错误:

    您应该在邮件正文中遵循以下模式: "key="+publicKey+"&nonce="+String.valueOf(nonce)+"&signature="+signature 至于额外的参数,你应该把它们附加到上面指定的正文字符串上。

    希望它仍然对你有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-12
      • 1970-01-01
      • 2015-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多