-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Binance private api issue #3057
Comments
Binance says that your apiKey format is invalid: So, you should really check it first... exchange = ccxt.binance ({
'apiKey': 'YOUR_API_KEY',
'secret': 'YOUR_SECRET',
'enableRateLimit': True, # ←---- in your best interest: https://github.com/ccxt/ccxt/wiki/Manual#rate-limit
})
print(exchange.fetch_balance()) Does this work with your keys? Make sure that you didn't misplace the apiKey and the secret, also make sure that both are 64-characters long. |
I'm sorry. That was my fail. |
@hexeraction thx for reporting back anyways ) |
I have encountered the same problem, what is the solution? |
|
@tye002 also, check that your the names for params that you're using for credentials are exactly |
@kroitor Thanks, I found the problem, it's about something related to the time mismatch. I tried this code, now it's working. |
@tye002 thx for reporting back! However, you said you have the same problem and the error message discussed in this issue: |
@kroitor Correct, there were two errors, the other one is about the timestamp. I thought this one :{"code":-2014,"msg":"API-key format invalid."} is the root problem. Thanks |
I get the same error: {"code":-2014,"msg":"API-key format invalid."}. The problem is that I don't always get the error, at the beginning it's working but after some time it returns that error. Can anyone help with a solution? Traceback (most recent call last): During handling of the above exception, another exception occurred: Traceback (most recent call last): During handling of the above exception, another exception occurred: Traceback (most recent call last): During handling of the above exception, another exception occurred: Traceback (most recent call last): |
@rs-g how do you initialize the exchange? Do you have the rate-limiter enabled? Can you paste the minimal smallest code to reproduce the issue (without your keys) ? |
@kroitor thanks alot for the quick reply exchange_id = 'binance'
exchange_class = getattr(ccxt, exchange_id)
exchange = exchange_class({
'apiKey': 'key here',
'secret': 'secret key here',
'timeout': 30000,
'enableRateLimit': True,
# for futures
'options': {
'defaultType': 'future',
},
})
exchange.fetch_balance().get('USDT').get('free') Like I said, I don't always get the error and I can't reproduce it, I get the error at random times |
This error could indicate a networking glitch on the client-side:
It can be caused by PyCharm if you're launching the code from the IDE. Is that the case? If yes, can you do multiple retries to try and trigger it on purpose? Also, you can do multiple tries running that same script outside of PyCharm (with pure Python) to see if it throws that error at some point. Apart from that, it may also be related to the frequency of launching that script. Because if the error is thrown after several successful consequent attempts, that could mean that you're not respecting the rate-limit. So, it also depends on whether the script is working continuously with the same exchange instance, or if the script is always recreating the exchange instance (effectively dropping the rate-limiting). In short, as documented in the Manual, CCXT will first try to load the list of markets and currencies from the exchange upon your first call to a unified method like So, in order to answer your question we need a lot more details (much more info, everything you can share). Also, reaching out to Binance techsupport might help as well, since they know better if they blocked you occasionally for any reason. |
Yes I was using PyCharm but I have tried with pure Python and I got the same error I have managed to reproduce the error above with this code: def test():
return exchange.fetchBalance().get('USDT').get('free')
while True:
print(test())
time.sleep(16*60) And right now I get another error: ccxt.base.errors.ExchangeError: GET https://fapi.binance.com/fapi/v1/exchangeInfo HTTPSConnectionPool(host='fapi.binance.com', port=443): Max retries exceeded with url: /fapi/v1/exchangeInfo (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x1031d7100>: Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known')) and I cannot access binance.com |
@rs-g it looks like an issue on binance's side, i can't open that URL on my machine either... Must be a temporary glitch or something. I can confirm, it's not available right now. |
@kroitor thank you for the support. What about my above code, seems like if I wait a little more time without doing any action on exchange. and then try to exchange.fetchBalance I get that error from my first post. Any thoughts? |
@rs-g it fails because of this other internal error, it's a cascade of errors that is initiated by the unavailability of the markets in the first place. So, |
@kroitor I will rephrase, I have tried this code when binance was working: def test():
return exchange.fetchBalance().get('USDT').get('free')
while True:
print(test())
time.sleep(16*60) I got the following result: first time when it entered in the while loop, test() returned the correct balance |
@rs-g are you having the same error at the moment? Because if the exchange is not fully available, one request can succeed and the other one can fail, randomly.
|
@kroitor at this moment I can not test it at all because binance is not accessible. I will try when it goes back online. but the last working test (before binance went down) was this:
|
@rs-g ok, let me know if you can reproduce it after a while, I will look into it. Unfortunately, I can't debug it now due to the unavailable API. I suspect a tmp connectivity issue on Binance's side. If it does not go away after a while – plz report back. Thx! |
@rs-g looks like it's up again, though, i'm not sure if it's stabilized: https://fapi.binance.com/fapi/v1/exchangeInfo. Anyways, let me know if you still experience difficulties with it. |
here is the full code: import ccxt
import time
exchange_id = 'binance'
exchange_class = getattr(ccxt, exchange_id)
exchange = exchange_class({
'apiKey': 'api',
'secret': 'secret',
'timeout': 30000,
'enableRateLimit': True,
# for futures
'options': {
'defaultType': 'future',
},
})
def get_max_position_available():
return exchange.fetchBalance().get('USDT').get('free')
while True:
print(get_max_position_available())
time.sleep(16*60) here is the output:
Am I doing something wrong? PS: If I change the sleep to 60, it's working |
@rs-g what's your version of CCXT? (it does work for me with proper keys + the most recent version of the lib + python3.7) |
@kroitor when I go to project interpreter in PyCharm, ccxt version is 1.26.9 and Python version is 3.8 You had the same sleep time as me and you had 2 results in a raw in the while loop? |
@rs-g i mean the actual runtime version: import ccxt
import time
exchange_id = 'binance'
exchange_class = getattr(ccxt, exchange_id)
exchange = exchange_class({
'apiKey': 'api',
'secret': 'secret',
'timeout': 30000,
'enableRateLimit': True,
# for futures
'options': {
'defaultType': 'future',
},
})
def get_max_position_available():
return exchange.fetchBalance().get('USDT').get('free')
print('CCXT version:', ccxt.__version__) # ←--------------------- add this
while True:
print(get_max_position_available())
time.sleep(16*60)
I had the same sleep as you, and now it's sleeping for 16 minutes (your delay specifically) before doing the second iteration of the loop, so i have to wait a bit before i can confirm. It did return the first response properly on the first iteration. |
@rs-g yes, it returned the second response properly to me:
You may want to check your IP address whitelists – do you have IP whitelisting enabled for the API keypair in question? (the IPs are configurable with binance website) |
I have updated the CCXT. it prints: CCXT version: 1.26.43 I will do one more test with this version, thanks a lot for your result I have the IP whitelisted because the first iteration of while prints the correct result |
@rs-g in that case I suspect PyCharm – try running the same script in pure python in console, outside of PyCharm to see if it makes a difference. You tried with pure python already, but that was a different connectivity error due to the exchange unavailability. I mean retry in pure python again from here. |
@rs-g let me know if this helps:
? |
@rs-g lol, it crashed for me on the third iteration in pure python:
I suggest to move this discussion here: #6807 ← let's proceed to resolving this issue, it's the same connectivity problem again. |
@kroitor you guys have other errors not the same as mine. Yours is because of the binance past downtime I guess
I did these already. I have the same problem when I started the script with pure Python.
I can't get past that 16 mins sleep, it always chases after the sleep. |
@rs-g https://twitter.com/cz_binance/status/1250411130289152001 The unavailability can manifest itself in many different ways, including random error messages on a breakdown. This really is an issue on Binance's side. We just need to wait until they resolve it. |
@kroitor it's just too strange that I get this error each time after sleep (16 mins) and I don't get any error if the sleep is 60 seconds, it's working perfectly fine :| Do I need to create another instance if I don't use the old one for 16 mins? |
Yes, that is strange, however, I cannot explain that other than with just random chance (like 2 of every 5 handshake requests work, other 3 requests fail, for example). The first iteration works for me the first time, but not always.
No. You should reuse the existing instance, like you do. In other words, your code should work regardless of the sleeping timeout, because even if you remove it completely – you have the built-in rate-limiter enabled. The problem is related to the underlying HTTPS transport, and it is most likely caused by the internal thresholds and timers inside the HTTPS implementation, originating with a bad response or no proper response from the exchange in the first place. |
Actually, it can be explained rather easily: if your handshake succeeds, the established connection is kept-alive (the keep-alive option is enabled for a few minutes by default by the underlying OSI stack). If you then keep sending requests each new request also extends the lifetime of the connection and keeps the connection alive. However, if you wait for 16 minutes – the exchange or the TCP/HTTPS client or server can drop it. So, on your second call the handshake that has to be done again can fail. This is why multiple continuous frequent requests work (if the handshake was successful), but some rare occasional requests can fail (this is random since binance is not fully available for SSL handshakes). To check the theory, you can run this: import ccxt
import time
exchange_id = 'binance'
exchange_class = getattr(ccxt, exchange_id)
exchange = exchange_class({
'apiKey': 'api',
'secret': 'secret',
'timeout': 30000,
'enableRateLimit': True,
# for futures
'options': {
'defaultType': 'future',
},
})
def get_max_position_available():
return exchange.fetchBalance().get('USDT').get('free')
while True:
try: # ←--------------------- add this
print(get_max_position_available())
except Exception as e: # ←--------------------- add this
print(e) # print the error and retry on next iteration using the same exchange instance
print('sleeping...')
time.sleep(16*60) ↑ If it first works, then fails, then works again in a loop – that proves the above theory. |
@kroitor makes sense, I'm trying this. Result:
|
@rs-g yeah, that's exactly what we thought, right? But it seems that they have mostly fixed it already, so you just want to wait a bit until the updates propagate over the globe and reach your ISP. https://twitter.com/cz_binance/status/1250432336774086660 |
@kroitor yep, thanks a lot for your help |
Hello.
I've tried to place an order on Binance but got following error. My api key is correct and all permissions already done.
My code looks like
connection = ccxt.binance({'apiKey': api, 'secret': secret}) print(connection.fetch_open_orders(symbol="ETH/BTC"))
And errors looks like :
equests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: https://api.binance.com/api/v3/openOrders?timestamp=1528282115226&recvWindow=5000&symbol=ETHBTC&signature=mysignature
and
ccxt.base.errors.ExchangeError: binance: unknown error code: {"code":-2014,"msg":"API-key format invalid."} -2014
The text was updated successfully, but these errors were encountered: