Cryptopia API对Sell的请求不好

时间:2017-08-13 03:04:21

标签: python python-2.7 api request

def sell():
    #Market: The market symbol of the trade e.g. 'DOT/BTC' (not required if 'TradePairId' supplied)
    #TradePairId: The Cryptopia tradepair identifier of trade e.g. '100' (not required if 'Market' supplied)
    #Type: the type of trade e.g. 'Buy' or 'Sell'
    #Rate: the rate or price to pay for the coins e.g. 0.00000034
   #Amount: the amount of coins to buy e.g. 123.00000000
   #SubmitTrade', {'Market': Market, 'Type': Type, 'Rate': Rate, 'Amount': Amount
    c = cryptopia(key,secret)
    c.SubmitTrade('CHC/BTC','Sell','1','.1')

所以,在这个我试图卖掉.1 chc为1 btc我正在使用这个api https://pastebin.com/nL089zwF的包装器。当我运行当前方法时,我得到( Response ): {"Success":false,"Error":"Bad Request","Data":null} 谢谢。

1 个答案:

答案 0 :(得分:1)

我修改了api_query方法并设法从api获得有效的响应。但是,代码远非最佳,您应该考虑使用其他库。

原始代码:

def api_query(self, method, values, req = None ):
    if not req:
        req = {}
    #print "def api_query( method = " + method + ", req = " + str( req ) + " ):"
    time.sleep(1)
    if method in self.public:
        url = 'https://www.cryptopia.co.nz/api/'
    elif method in self.private:
        url = 'https://www.cryptopia.co.nz/api/'
    else:
        return 'Call Not Identified - Something Went Wrong.'

    url += method + '?' + urllib.urlencode(values)

    if method not in self.public:
        url = "https://www.cryptopia.co.nz/Api/" + method
        nonce = str( int( time.time() ) )
        post_data = json.dumps( req );
        m = hashlib.md5()
        m.update(post_data)
        requestContentBase64String = base64.b64encode(m.digest())
        signature = self.key + "POST" + urllib.quote_plus( url ).lower() + nonce + requestContentBase64String
        hmacsignature = base64.b64encode(hmac.new(base64.b64decode( self.secret ), signature, hashlib.sha256).digest())
        header_value = "amx " + self.key + ":" + hmacsignature + ":" + nonce
        headers = { 'Authorization': header_value, 'Content-Type':'application/json; charset=utf-8' }
        r = requests.post( url, data = post_data, headers = headers )
    response = r.text

    print "( Response ): " + response
    return response.replace("false","False").replace("true","True").replace('":null','":None' )

修改后的代码:

def api_query(self, method, values={}, req={}):
    time.sleep(1)
    if method in self.private:
        url = "https://www.cryptopia.co.nz/Api/" + method
        nonce = str(int(time.time()))
        requestContentBase64String = base64.b64encode(hashlib.md5(json.dumps(values)).digest())
        signature = self.key + "POST" + urllib.quote_plus(url).lower() + nonce + requestContentBase64String
        hmacsignature = base64.b64encode(hmac.new(base64.b64decode(self.secret), signature, hashlib.sha256).digest())
        header_value = "amx " + self.key + ":" + hmacsignature + ":" + nonce
        headers = { 'Authorization': header_value }
        r = requests.post(url, json = values, headers=headers)
    elif method in self.public:
        url = 'https://www.cryptopia.co.nz/api/' + method 
        r = requests.get(url, params=values) 
    else: 
        return 'Call Not Identified - Something Went Wrong.'
    response = r.content
    print "( Response ): " + response
    return response.replace("false","False").replace("true","True").replace('":null','":None' )

<强>注意:
我没有彻底测试代码,我无法保证其安全性或功能性;我只是提供一个有效的例子。