不接受C-CEX API的签名

时间:2017-06-22 12:16:51

标签: python rest api authentication

我试图通过C-CEX.com验证自己,访问测试account's wallet information

我一丝不苟地跟着the documentation,并在网上搜索了代码示例,但无济于事(除了this过时的库) - 结果总是这样:

>>>make_sign_and_request('getbalances')
{'success': False, 'result': None, 'message': 'INVALID_SIGNATURE'}

由于似乎没有找到Python示例,and authentication is explained in PHP only(我的代码基于以下代码),整个过程变得更加复杂。

我的签名功能:

import requests
import time
import urllib
import hashlib
import hmac

# Keys for the test account
API_KEY = '7EAEBD233321AB48C8B5EAB58FF8849B'
API_SECRET = '139057C3B770C2DBCFCFFDD0D8A56E3F'


def make_sign_and_request(endpoint, **request_kwargs):
    req_kwargs = {'url': 'https://c-cex.com/t', 'headers': {}, 'files': {},
                  'data':  {}, 'params': {}, 'auth': {}, 'cookies': {},
                  'hooks': {}, 'json': {}}
    req_kwargs.update(request_kwargs)

    # Prepare Payload arguments
    nonce = str(int(time.time() * 1000))

    params = req_kwargs.pop('params')
    params['apikey'] = API_KEY
    params['nonce'] = nonce
    post_params = params
    post_params.update({'nonce': nonce, 'a': endpoint})
    url_encoded_params = urllib.parse.urlencode(post_params)
    url = req_kwargs['url'] + '/api.html?' + url_encoded_params
    print(url)
    # generate signature
    sig = hmac.new(API_SECRET.encode('utf-8'), url.encode('utf-8'),
                   hashlib.sha512).digest()

    # update req_kwargs keys
    req_kwargs['headers'] = {'apisign': sig}
    req_kwargs['url'] = url
    return requests.request('GET', **req_kwargs)

print(make_sign_and_request('getbalances').json())

现在,我不是PHP向导,但我非常有信心我已经非常准确地复制了python中的代码。嗯,显然不是,否则将接受签名。

api键已启用,应按预期工作。

预期结果应如下according to the documentation:

{"success":true,"message":"","result":{"Currency":"BTC","Balance":0.00000000,"Available":0,"Pending":0.00000000,"CryptoAddress":""}}

1 个答案:

答案 0 :(得分:0)

问题已经解决:

hexdigest()对象上添加对hmac的调用可解决此问题。