Rustam
Rustam

Reputation: 21

Problems with signature in MEXC API withdraw

So I'd been trying to write a withdraw function for MEXC and can't pass the reponse: {'code': 700004, 'msg': "Mandatory parameter 'signature' was not sent, was empty/null, or malformed."}

I'd tried a lot of modifications but none of them seems to work for me.

Here is my code:

import requests
import hmac
from urllib.parse import urlencode, quote
from collections import OrderedDict
import hashlib
from datetime import datetime
import time

access_key = "(MY_ACCESS_KEY)"
secret_key = "(MY_SECRET_KEY)"

def generate_signature(api_key, api_secret, req_time, sign_params=None):
    if sign_params:
        sign_params = urlencode(sign_params, quote_via=quote)
        to_sign = f"{api_key}{req_time}{sign_params}"
    else:
        to_sign = f"{api_key}{req_time}"

    sign = hmac.new(api_secret.encode('utf-8'), to_sign.encode('utf-8'), hashlib.sha256).hexdigest()
    return sign


url = "https://api.mexc.com/api/v3/capital/withdraw/apply"
req_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
symbol = "(MY_SYMBOL)"
balance = (MY_AMOUNT)
quantity = 0.5
signature = generate_signature(access_key, secret_key, req_time)
sign_params = {
    #'api-key': access_key,
    "symbol": symbol,
    "network": "(MY_NETWORK)",
    "address": "(MY_ADDRESS)",
    "memo": "(MY_MEMO)",
    "amount": str(balance)
}

headers = {
    "apiKey": access_key,
    "symbol": symbol,
    "network": (MY_NETWORK),
    "address": "(MY_ADDRESS)",
    "memo": "(MY_MEMO)",
    "amount": str(balance),
    "signature": signature
}

response = requests.get(url, headers=headers).json()
print(response)

(there is some trash in the code from some methods that didn't work) I would really appreciate any help!

Upvotes: 0

Views: 1800

Answers (1)

Jon Delavera
Jon Delavera

Reputation: 1

Looks like you are writing the timestamp incorrectly. Needs to be a proper timestamp, from epoch and in milliseconds.

Try:

import time

now=time.time()
req_time=int(now*1000)

PS. I'm using 'int' to cast to type 'Long' as is what is expected

Upvotes: 0

Related Questions