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

binance.exceptions.BinanceAPIException: APIError(code=-1022): Signature for this request is not valid.

Open includeleec opened this issue 9 years ago • 42 comments

always...

includeleec avatar Sep 09 '17 17:09 includeleec

@includeleec can you give an example of how you're using the API.

sammchardy avatar Sep 10 '17 04:09 sammchardy

from binance.client import Client

class Monitor:
    def __init__(self):
        self.bac = Client(KEY,SERCRET)
    def my_balance(self):
        print(self.bac.get_account(recvWindow=RECV_WINDOW))


m = Monitor()
m.my_balance()

sometimes ok,sometimes throw error.

includeleec avatar Sep 10 '17 04:09 includeleec

@includeleec can you check the value you're using for recvWindow, the API docs indicate it should be an int for the number of milliseconds the request is valid for.

If I send a string of say "4000" then I also get the Signature invalid error back from the Binance API.

sammchardy avatar Sep 10 '17 10:09 sammchardy

RECV_WINDOW=6000000, this setting i copy from binance example

includeleec avatar Sep 10 '17 16:09 includeleec

#!/usr/bin/env python

api_key = 'key_here'
api_secret = 'secret_here'

from binance.client import Client
client = Client(api_key, api_secret)

time_res = client.get_server_time()

RECV_WINDOW=6000000

class Monitor:
    def __init__(self):
        self.bac = Client(api_key,api_secret)
    def my_balance(self):
        print(self.bac.get_account(recvWindow=RECV_WINDOW))


m = Monitor()
m.my_balance()

It's working for me, did you set up your API key correctly?

jmynes avatar Sep 14 '17 00:09 jmynes

Hi,

same issue with invalid invalid signature.

Thanks

laurent101 avatar Sep 23 '17 22:09 laurent101

@laurent101 please provide examples to add to the discussion.

There are a range of things that can cause this, some that I'm aware of are listed in the FAQ here https://github.com/sammchardy/python-binance#user-content-faq

sammchardy avatar Sep 23 '17 22:09 sammchardy

I ran the code as posted above by @jmynes and ended up with an invalid signature error message. Both my key and secret (both strings) are correct.

laurent101 avatar Sep 23 '17 22:09 laurent101

I found the issue which is related to the API version issue I posted. I had added and changed the version of the REST API from 'v1' to 'v3' which is the update on the Binance website. As @sammchardy correctly pointed out this requires an amended signature. Sorry for the inconvenience caused.

laurent101 avatar Sep 23 '17 23:09 laurent101

Im encountering the same issue on v1 as @includeleec even when I add "recvWindow=6000000" to the parameters. the signature is sometimes valid and sometimes not valid.

junw3i avatar Sep 28 '17 00:09 junw3i

same issue. seems to fail randomly

junkheadd avatar Oct 09 '17 22:10 junkheadd

Had this issue, however appending recvWindow=5000 to the request seems to fix it for me.

Very strange especially since the API docs state that

An additional parameter, recvWindow, may be sent to specific the number of milliseconds after timestamp the request is valid for. If recvWindow is not sent, it defaults to 5000 millisecond.

and yet the signature is invalid unless I provide a recvWindow ...

EDIT: Seems like get_account doesn't work even with recvWindow=5000

frosty00 avatar Oct 15 '17 17:10 frosty00

For anyone still running into this issue, I've been referencing sammchardy's extremely helpful code here as a template for my own project, and I was continually running into this error as well. Here is an example standalone code snippet that I was finally able to get working:

api_key = '' api_secret = ''

request_url = 'https://api.binance.com/api/v3/openOrders?'

timestamp = int(time.time() * 1000) symbol = 'BNBBTC'

querystring = urlencode({'symbol' : symbol, 'timestamp' : timestamp})

signature = hmac.new(api_secret.encode('utf-8'), querystring.encode('utf-8'), hashlib.sha256).hexdigest()

request_url += querystring + '&signature=' + signature

r = requests.get(request_url, headers={"X-MBX-APIKEY": api_key})

shammy12 avatar Nov 23 '17 07:11 shammy12

Has anyone found a fix for this yet? I am running into it when calling .getAccount(). I've tried setting RECV_WINDOW manually without success.

tevonsb avatar Dec 27 '17 07:12 tevonsb

I'm also running into this intermittently when initiating various sales.

5outh avatar Dec 27 '17 21:12 5outh

I've updated some dependencies in v0.5.10 including certifi which has resolved this issue for me for the moment. Let me know if it helps.

sammchardy avatar Dec 27 '17 23:12 sammchardy

I just updated everything today but am also still receiving this error when calling the withdraw method. All the signed GET withdraw api functions seem to work fine though (deposithistory, withdrawhistory, depositaddress). I've tried increasing the recvWindow up to 6000000 ms but with no success.

dafrizzy avatar Dec 28 '17 20:12 dafrizzy

getting this error when placing orders, has anyone been able to find a solution? tried @shammy12's solution but got a similar error >> " {'code': -1022, 'msg': 'Signature for this request is not valid.'}"

a-mekon avatar Dec 29 '17 07:12 a-mekon

@meko0019 Can you give an example function call that produces that error? I'm hitting it all over the place, but one common roadblock I'm seeming to hit is when selling ADA. ADA is only buyable/sellable in whole increments; I'm wondering if the API is expecting a decimal for the purposes of the signature...

(FWIW I have updated and am still seeing this pretty commonly as well)

EDIT: I could not reproduce the bug by making that change (int -> float). Is there some way for an extra parameter to be included in the API response/signature by accident?

5outh avatar Dec 30 '17 13:12 5outh

I have released v0.5.13 which ensures orders are correct when generating signature and sending to server. This seems to resolve issues in my tests.

Let me know if it's still occurring.

sammchardy avatar Jan 01 '18 10:01 sammchardy