mexcdevelop / mexc-api-sdk

MIT License
173 stars 70 forks source link

ERROR: PARAMETER API KEY IS REQUIRED USING PYTHON CONNECTION TO MEXC #48

Open Dotijo opened 11 months ago

Dotijo commented 11 months ago

Below is the code am having isue executing having pasted my API KEY and the SECRET KEY:

import time import requests import hashlib import hmac

def create_signed_request(api_key, secret_key, method, endpoint, params={}): base_url = 'https://www.mexc.com' request_url = f"{base_url}{endpoint}" timestamp = int(time.time() * 1000) params['api_key'] = api_key params['req_time'] = timestamp sorted_params = sorted(params.items()) query_string = "&".join([f"{k}={v}" for k, v in sorted_params])

if method == 'GET':
    to_sign = f"{method}{request_url}?{query_string}"
else:
    to_sign = f"{method}{request_url}"

signature = hmac.new(secret_key.encode(), to_sign.encode(), hashlib.sha256).hexdigest()
headers = {
    'Content-Type': 'application/x-www-form-urlencoded',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',
    'Cookie': 'locale=en_US',
    'Authorization': f"{signature}",
}

if method == 'GET':
    response = requests.get(request_url, headers=headers)
else:
    response = requests.post(request_url, headers=headers, data=query_string)

return response.json()

def get_account_balance(api_key, secret_key): endpoint = '/open/api/v2/account/info' method = 'GET' response = create_signed_request(api_key, secret_key, method, endpoint)

if 'code' in response and response['code'] == '200':
    return response['data']['spot']['list']
else:
    print("Error in getting account balance:", response)
    return None

def main():

Read API key and secret key from the user

API_KEY='my api key is pasted here'
SECRET_KEY='my secret key pasted here'

# Get account balance
account_balance = get_account_balance(API_KEY, SECRET_KEY)
if account_balance:
    print("Account Balance:")
    for balance in account_balance:
        asset = balance['currency']
        free_balance = balance['available']
        locked_balance = balance['frozen']
        print(f"{asset}: Free={free_balance}, Locked={locked_balance}")
else:
    print("Failed to retrieve account balance.")

if name == "main": main()