Rest API参数错误

Rest API参数错误

问题描述:

我正在尝试编写一个加密货币交换的Python包装器。Rest API参数错误

#!/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() 

响应:u'status': 102, u'msg': u'', u'data': u''

根据API响应描述:代码102意味着

参数错误。所需参数丢失或格式错误。

我相信我有所有必需的参数1.关键2.随机3.签名。我是否在错误的位置或以错误的格式传送有效载荷?不幸的是,他们的文档不是很清楚,我是初学者。

谢谢。

+0

有没有试过邮递员在代码之前测试你的请求? – Gahan

+0

我下载邮递员,试图通过系统运行我的请求。我不确定我的负载是否属于授权,标题或正文标签。 O.Auth 1.0与访问令牌,令牌密钥最相似,但md5散列不是标准功能。 – tada23

+0

[offtopic]但我有很多问题to coinnest.co.kr家伙。为什么你会使用一些模糊的哈希算法?为什么你会使用隐含的错误代码而不是HTTP错误代码?我感觉你的痛苦OP。[/ offtopic] – vittore

文档很糟糕,但看起来您应该用md5(secret)签署您的消息,并将key设置为您的公钥,该公钥与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)]) 

我还建议使用有序的词典来强制执行参数顺序。

+0

是的。那就对了。 –