Last updated
Content-Type: application/json
MF-ACCESS-KEY: 您的API Key
MF-ACCESS-SIGN: 请求签名(见后续签名规则)
MF-ACCESS-TIMESTAMP: 当前时间戳sign = timestamp + method + requestPathpip install requestsimport requests
import time
import base64
import hmac
import json
from datetime import datetime
# API 配置信息
BASE_URL = "https://api.mefree.net"
API_KEY = "your_api_key"
SECRET_KEY = "your_secret_key"
# 签名生成函数
def generate_signature(timestamp, method, request_path, secret_key):
message = f"{timestamp}{method}{request_path}"
mac = hmac.new(secret_key.encode('utf-8'), message.encode('utf-8'), digestmod='sha256')
return base64.b64encode(mac.digest()).decode()
# 当前 UTC 时间戳
timestamp = datetime.utcnow().isoformat(timespec='milliseconds') + 'Z'
# 设置请求信息
method = "GET"
request_path = "/api/config"
url = BASE_URL + request_path
body = None # GET 请求无需 Body
# 生成签名
signature = generate_signature(timestamp, method, request_path, SECRET_KEY)
# 请求头
headers = {
"Content-Type": "application/json",
"MF-ACCESS-KEY": API_KEY,
"MF-ACCESS-SIGN": signature,
"MF-ACCESS-TIMESTAMP": timestamp,
}
# 发起请求
response = requests.get(url, headers=headers)
if response.status_code == 200:
print("账户信息:", response.json())
else:
print("请求失败:", response.status_code, response.text)
# 生成签名
timestamp = datetime.utcnow().isoformat(timespec='milliseconds') + 'Z'
method = "POST"
request_path = "/api/order?quantity=65000&target_address=TDYk....4rnPfkdPZ&period=1"
signature = generate_signature(timestamp, method, request_path, SECRET_KEY)
# 请求头
headers = {
"Content-Type": "application/json",
"MF-ACCESS-KEY": API_KEY,
"MF-ACCESS-SIGN": signature,
"MF-ACCESS-TIMESTAMP": timestamp,
}
# 发起请求
response = requests.post(BASE_URL + request_path, headers=headers, json=body)
if response.status_code == 200:
print("订单已创建:", response.json())
else:
print("请求失败:", response.status_code, response.text)