Skip to content

Pls help correct this code to zero error using mexc exchanger documentation #51

Description

@Dotijo

import time
import requests
import hashlib
import hmac
import json

def create_signed_request(api_key, secret_key, method, endpoint, params={}):
base_url = 'https://api.example.com'
#50 Replace with the API base URL
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])

# Fix the ternary if-else statement
to_sign = f"{method}{request_url}?{query_string}" if method == 'GET' else f"{method}{request_url}"

signature = hmac.new(secret_key.encode(), to_sign.encode(), hashlib.sha256).hexdigest()
headers = {
    'X-API-KEY': api_key,
    'Content-Type': 'application/json',
    'Authorization': f"{signature}",
}
if method == 'GET':
    response = requests.get(request_url, headers=headers)
else:
    response = requests.post(request_url, headers=headers, data=json.dumps(params))

if response.status_code == 200:
    return response.json()  # Return the JSON response
else:
    print(f"Failed to retrieve data. Status Code: {response.status_code}")
    return None

def main():
# Read API key and secret key from the user
API_KEY = 'my api key here'
SECRET_KEY = 'my secret key here'

# Example parameters for the /api/v3/rebate/referCode endpoint
params = {
    'param1': 'value1',
    'param2': 'value2',
}

# Make the signed request to the /api/v3/rebate/referCode endpoint
response = create_signed_request(API_KEY, SECRET_KEY, 'POST', '/api/v3/rebate/referCode', params=params)
if response:
    print("Response:")
    print(json.dumps(response, indent=4))
else:
    print("Failed to retrieve data.")

if name == "main":
main()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions