pythonpython-requests

How to connect python to coinex api v2?


I saw the below links, However, all of them use the v1 API and I'd like to use the v2 API.
Connecting to Coinex API issue
how to connect Coinex exchange with API?
How To Trade With CoinEx API?

So, I use the v2 API documentations as below code in python3:

import time
import requests
import hmac
import hashlib
baseUrl = 'https://api.coinex.com'
requestPath = '/v2/account/info'
timestamp = int(time.time()*1000)
preparedString = "GET"+requestPath+str(timestamp)
accessId = '???'
secretKey = '???'
signed_str = hmac.new(bytes(secretKey, 'latin-1'), bytes(preparedString, 'latin-1'), hashlib.sha256).hexdigest().lower()
params = {'X-COINEX-KEY':accessId,
         'X-COINEX-SIGN':signed_str,
         'X-COINEX-TIMESTAMP':timestamp}
response = requests.get(baseUrl+requestPath,
                        params=params
                        )
response.json()

However, I get this error:

{'code': 11003, 'data': {}, 'message': 'Access ID does not exist'}

The error code 11003 doesn't exist in coinex error handling.

Why does it say Access ID does not exist?

I am sure the access_id and secret_key are correct and I can connect to Coinex and get the public data, such as the market info (e.g. /futures/ticker?market=BTCUSDT) without any problems.


Solution

  • After contacting the Coinx support team, they introduced an API V2 coding demo, and my problem has been solved by using it. The problem was in assigning the header section (request header must includes the Content-Type field).

    headers["Content-Type"] = "application/json; charset=utf-8"
    

    I should have used a header instead of a parameter in requests.get. The request path does not need to be added to the URL, just having it in the signed_str is enough.

    headers = {'X-COINEX-KEY':accessId,
             'X-COINEX-SIGN':signed_str,
             'X-COINEX-TIMESTAMP':timestamp
             'Content-Type':'application/json;chrset=utf-8'}
    response = requests.get(baseUrl,
                            headers = headers
                            )
    

    And also the signature generation should change as below:

    signed_str = hmac.new(bytes(secretKey, 'latin-1'), msg=bytes(preparedString, 'latin-1'), digestmod=hashlib.sha256).hexdigest().lower()
    

    The timestamp should be as string:

    timestamp = str(time.time()*1000)