Open crazalchemist opened 2 years ago
I have exacty the same issue.
this way it doesn't give this error:
params_in = []
params0 = {
'symbol':'BTCUSD_PERP',
'side':'BUY',
'type':'STOP_MARKET',
'stopPrice':'48000',
'quantity':'1'
}
params1 = {
'symbol':'BTCUSD_PERP',
'side':'BUY',
'type':'STOP_MARKET',
'stopPrice':'49000',
'quantity':'1'
}
params2 = {
'symbol':'BTCUSD_PERP',
'side':'BUY',
'type':'STOP_MARKET',
'stopPrice':'50000',
'quantity':'1'
}
params_in.append(params0)
params_in.append(params1)
params_in.append(params2)
client.futures_coin_place_batch_order(batchOrders=params_in)
I'm getting an error "Invalid symbol" with BTCBUSD_PERP
futures_place_batch_order() worked for me
await client_1_a.futures_place_batch_order(**params_in)
binance.exceptions.BinanceAPIException: APIError(code=-1022): Signature for this request is not valid.
tested by 1.0.16
Maybe because the difference between request and aiohttp
I got the same error on AsyncClient with futures_place_batch_order
Facing the same issue. Has anyone found a workaround?
The issue comes when using AsyncClient
Okay, because aiohttp is encoding the URL, and due to the changes in params because of the encoding, the signature is getting mismatched. To overcome this, I wrote a working code in Async by disabling the aiohttp encoding.
import aiohttp
import asyncio
import json
import hashlib
import hmac
import time
from urllib.parse import urlencode, quote
from yarl import URL
BASE_URL = 'https://fapi.binance.com'
apiKey = "Your_Api_Key"
secret = "Your_Secret_Key"
def get_signature(secret, query_string):
return hmac.new(secret.encode('utf-8'), query_string.encode('utf-8'), hashlib.sha256).hexdigest()
async def batch_order():
PATH = '/fapi/v1/batchOrders'
timestamp = int(time.time() * 1000)
batch_orders = json.dumps([
{
"symbol": "BTCUSDT",
"side": "BUY",
"type": "LIMIT",
"timeInForce": "GTC",
"quantity": "0.01",
"price": "25000",
},
{
"symbol": "BTCUSDT",
"side": "BUY",
"type": "LIMIT",
"timeInForce": "GTC",
"quantity": "0.01",
"price": "30000",
}
])
# Preparing the query string including batchOrders
query_params = {
'batchOrders': batch_orders,
'timestamp': timestamp
}
query_string = urlencode(query_params, quote_via=quote)
# Generating the signature
signature = get_signature(secret, f"batchOrders={quote(batch_orders)}×tamp={timestamp}")
# Final URL with query params
url = f"{BASE_URL}{PATH}?{query_string}&signature={signature}"
headers = {
'X-MBX-APIKEY': apiKey
}
async with aiohttp.ClientSession() as session:
async with session.post(URL(url,encoded=True), headers=headers) as response:
response_text = await response.text()
print(response_text)
async def main():
await batch_order()
if __name__ == "__main__":
asyncio.run(main())
This is working code but my interpretation towards this error is wrong. The reason we getting the signature error is because of params field. When I used data field the error got fixed.
Describe the bug The AsyncClient.futures_place_batch_order provide an "1022 INVALID_SIGNATURE" error
To Reproduce
Expected behavior A working order without signature error
Environment (please complete the following information): python3.7 / python3.9 debian 10 / Windows 10 python-binance tested on all major version from 1.0.0 New API keys with with spot and future enable just to be sure
Logs or Additional context Add any other context about the problem here.