这个错误表示您提供的API密钥无效,可能是由于以下原因导致的:
API密钥错误:请确保您使用的是正确的API密钥,包括密钥和密钥密码。
权限不足:请确保您的API密钥具有足够的权限来执行private_post_order_oco操作。
解决方法如下:
检查API密钥:请确保您的API密钥是正确的,并且没有任何拼写错误或者额外的空格。
检查权限:请确保您的API密钥具有足够的权限来执行private_post_order_oco操作。您可以在币安账户的API管理页面上查看和管理API密钥的权限。
如果您仍然遇到问题,可以尝试使用以下代码示例来调用private_post_order_oco方法:
import requests
import hashlib
import hmac
import time
api_key = 'YOUR_API_KEY'
secret_key = 'YOUR_SECRET_KEY'
base_url = 'https://api.binance.com'
def create_oco_order(symbol, side, quantity, price, stop_price, stop_limit_price, stop_limit_time_in_force, list_client_order_id=None):
endpoint = '/api/v3/order/oco'
params = {
'symbol': symbol,
'side': side,
'quantity': quantity,
'price': price,
'stopPrice': stop_price,
'stopLimitPrice': stop_limit_price,
'stopLimitTimeInForce': stop_limit_time_in_force,
'timestamp': int(time.time() * 1000)
}
if list_client_order_id:
params['listClientOrderId'] = list_client_order_id
query_string = '&'.join([f'{k}={v}' for k, v in params.items()])
signature = hmac.new(secret_key.encode(), query_string.encode(), hashlib.sha256).hexdigest()
headers = {
'X-MBX-APIKEY': api_key
}
url = f'{base_url}{endpoint}?{query_string}&signature={signature}'
response = requests.post(url, headers=headers)
return response.json()
symbol = 'BTCUSDT'
side = 'SELL'
quantity = 0.001
price = 55000
stop_price = 54000
stop_limit_price = 53500
stop_limit_time_in_force = 'GTC'
response = create_oco_order(symbol, side, quantity, price, stop_price, stop_limit_price, stop_limit_time_in_force)
print(response)
请确保将YOUR_API_KEY
和YOUR_SECRET_KEY
替换为您自己的API密钥。这段代码使用了Python的requests
库来发送POST请求,使用了HMAC-SHA256算法来生成签名,并在请求头中添加了API密钥。您可以根据自己的需要调整参数和功能。
希望这可以帮助您解决问题!