python-binance icon indicating copy to clipboard operation
python-binance copied to clipboard

BinanceAPIException: APIError(code=-1022): Signature for this request is not valid. - when trying to cancel order

Open liviuancas opened this issue 4 years ago • 7 comments

Hi, i'm trying to cancel an active order, i get the list of orders with

futures_get_open_orders()

and i try to cancel order like this : futures_cancel_order(symbol='EGLDUSDT', origClientOrderIdList=["asdasda"], timestamp=client.get_server_time()) or: .futures_cancel_order(symbol='EGLDUSDT', orderIdList=[1231321], timestamp=client.get_server_time())

but all the time i get this error:

Traceback (most recent call last): File "cancel_order.py", line 69, in close_order = client.futures_cancel_order(symbol=simbol, origClientOrderIdList=["asdasda"], timestamp=client.get_server_time()) File "/home/aaa/.local/lib/python3.6/site-packages/binance/client.py", line 4998, in futures_cancel_order return self._request_futures_api('delete', 'order', True, data=params) File "/home/aaa/.local/lib/python3.6/site-packages/binance/client.py", line 222, in _request_futures_api return self._request(method, uri, signed, True, **kwargs) File "/home/aaa/.local/lib/python3.6/site-packages/binance/client.py", line 197, in _request return self._handle_response() File "/home/aaa/.local/lib/python3.6/site-packages/binance/client.py", line 230, in _handle_response raise BinanceAPIException(self.response) binance.exceptions.BinanceAPIException: APIError(code=-1022): Signature for this request is not valid.

i have to mention that buy/get/userdata etc works.

Any suggestions?

thanks

liviuancas avatar Jan 16 '21 00:01 liviuancas

Dear liviuancas,

Method client.get_server_time() returns dictionary {'server Time': 1611500979032}, but timestamp supposed to be int.

markeyev avatar Jan 24 '21 15:01 markeyev

Doc says that these type of requests must be signed in a particular way https://github.com/binance/binance-spot-api-docs/blob/master/rest-api.md#signed-endpoint-examples-for-post-apiv3order

SIGNED (TRADE and USER_DATA) Endpoint security SIGNED endpoints require an additional parameter, signature, to be sent in the query string or request body. Endpoints use HMAC SHA256 signatures. The HMAC SHA256 signature is a keyed HMAC SHA256 operation. Use your secretKey as the key and totalParams as the value for the HMAC operation. The signature is not case sensitive. totalParams is defined as the query string concatenated with the request body.

But i can't find a way for launch these requests through the python library instead of using an external one, like requests

luponata avatar Feb 12 '21 22:02 luponata

I have the same issue here with futures_cancel_orders. Does anyone have any clue how to solve this?

irchamaji avatar Oct 15 '21 15:10 irchamaji

I have a similar problem with this: Please help!!! I get the same error when sending the order from a Heroku server as a response from a webhook triggered in Tradingview.

                    order = client_Binance.futures_create_order(
                        symbol="BTCUSDT",
                        side="BUY",
                        type="LIMIT",
                        timeInForce="GTC",
                        quantity="0.029",
                        price="28000.0",
                        recvWindow=6000000,
                        newClientOrderId = "LongTrd 1C5BTC2L",
                        timestamp = 1653043804435
                    )

However, if I send the webhook from my Mac (to the Heroku server) I do not get the same error.

It only occurs when the Heroku server tries to place the order in Binance as a result of a webhook coming from Tradingview and not my local Mac.

gtellezgiron avatar May 20 '22 11:05 gtellezgiron

Doc says that these type of requests must be signed in a particular way https://github.com/binance/binance-spot-api-docs/blob/master/rest-api.md#signed-endpoint-examples-for-post-apiv3order

SIGNED (TRADE and USER_DATA) Endpoint security SIGNED endpoints require an additional parameter, signature, to be sent in the query string or request body. Endpoints use HMAC SHA256 signatures. The HMAC SHA256 signature is a keyed HMAC SHA256 operation. Use your secretKey as the key and totalParams as the value for the HMAC operation. The signature is not case sensitive. totalParams is defined as the query string concatenated with the request body.

But i can't find a way for launch these requests through the python library instead of using an external one, like requests

I have the same question. How to include signature información inside the python library??

gtellezgiron avatar May 20 '22 11:05 gtellezgiron

You just need to remove spaces and encode the string. Do this:

from urllib.parse import quote
from json import dumps
    
    order_id_str = dumps(order_id).replace(" ", "")
    order_id_str = quote(order_id_str)
    self.client.cancel_order(symbol=pare, orderIdList=order_id_str)

mFreezetime avatar May 22 '22 07:05 mFreezetime

@sammchardy I have a similar problem with this: Please help!!! image image

janekaze avatar May 24 '23 14:05 janekaze