Reputation: 21
I'm trying to retrieve the wallet balances from the mexc.com cryptocurrency exchange. It works well on others such as Gate.io or Huobi thanks to their awesome documentation. I'm stuck on mexc.com however and confused on how to authenticate myself there.
Based on their documentation, I'd need to send a GET request to /open/api/v2/account/info. I'd also need to sign the request and this is where I'm stuck.
When a request requires a signature, I need to pass the API key, unix time stamp and a calculated signature as URL parameters. I have no clue though how to calculate that signature. Also I'd need to sign a string and pass it into the header.
This is their description:
Upvotes: 2
Views: 2666
Reputation: 11
Accounting to MEXC documentation https://mxcdevelop.github.io/apidocs/contract_v1_en/#access-to-url signature is combination of APIkey,secret,request_time and request params.
import hmac
from urllib.parse import urlencode, quote
from collections import OrderedDict
for spot account you can use:
def signature(self, req_time, sign_params=None):
if sign_params:
sign_params = urlencode(sign_params, quote_via=quote)
to_sign = "{}×tamp={}".format(sign_params, req_time)
else:
to_sign = "timestamp={}".format(req_time)
sign = hmac.new(self._secret.encode('utf-8'), to_sign.encode('utf-8'), hashlib.sha256).hexdigest()
return sign
and for future account you can use:
def signature(self, req_time, sign_params=None):
if sign_params:
sign_params = urlencode(OrderedDict(sorted(sign_params.items())), quote_via=quote)
to_sign = f"{self._access_id}{req_time}{sign_params}"
else:
to_sign = f"{self._access_id}{req_time}"
sign = hmac.new(self._secret.encode('utf-8'), to_sign.encode('utf-8'), hashlib.sha256).hexdigest()
return sign
Note that place of signature and APIKey are deferent in spot account request and future account request, for future API its in header while in spot API its in request params. for more detail you can read MECX API documentation .
Upvotes: 1