I'm trying to write a Python wrapper for a cryptocurrency exchange.
#!/usr/bin/python2.7
import hashlib
import hmac
import time
base_url = 'https://api.coinnest.co.kr'
class Coinnest():
def __init__(self, key, secret):
self.key = key
self.secret = secret
def get_balance(self):
url = base_url + '/api/account/balance'
nonce = str(int(time.time())*1000)
key = hashlib.md5(self.secret).hexdigest()
message = 'key={}&nonce={}'.format(self.key, nonce)
signature = hmac.new(key, message, hashlib.sha256).hexdigest()
payload = {'key': key, 'nonce': nonce, 'signature': signature}
r = requests.post(url, data=payload)
return r.json()
coinnest = Coinnest('','')
print coinnest.get_order_history()
Response: u'status': 102, u'msg': u'', u'data': u''
According to the API response description: Code 102 means
Parameter error. Required parameters are missing or in wrong format.
I believe I have all the required parameters of
Am I delivering the payload in the wrong location or in the wrong format? Unfortunately, their documentation is not very clear and I am a beginner.
Thank you.
答案 0 :(得分:1)
The docs are terrible, but it looks like you should be signing your message with md5(secret)
, and set key
to your public key, which is different from md5(secret)
.
from collections import OrderedDict
key = self.key
secret_md5 = hashlib.md5(self.secret).hexdigest()
signature = hmac.new(secret_md5, message, hashlib.sha256).hexdigest()
payload = OrderedDict([('key', key), ('nonce', nonce), ('signature', signature)])
I also recommend using an ordered dict to enforce parameter order.