sammchardy / python-kucoin

Kucoin REST and Websocket API python implementation
https://python-kucoin.readthedocs.io/en/latest/
MIT License
352 stars 148 forks source link

Getting KucoinAPIException UNAUTH: Invalid nonce too much #6

Closed priyen closed 6 years ago

priyen commented 6 years ago

Hi, I keep getting the above exception while trying to do recurring balance calls. It happens after a minute or so but It seems really random. Im not sure how to fix it

sammchardy commented 6 years ago

Hi @priyen I can't see any documentation around that issue.

Are you able to give me an example of the code you're using?

priyen commented 6 years ago

ending_kucoin_ltc = client_kucoin.get_coin_balance('LTC')['balance'] ending_kucoin_eth = client_kucoin.get_coin_balance('ETH')['balance']

The call works the first time, then keeps getting unauth unless I reinstantiate a new client

sammchardy commented 6 years ago

Hi @priyen from my investigation it seems the Kucoin API response times are inconsistent.

Responses that take longer than 10 seconds return a 504 gateway timeout message that I need to handle properly. Response that take between 6 and 10 seconds return the UNAUTH: Invalid nonce error.

If the response returns in the less than 6 seconds then it works properly.

So at the moment the only fix may be retrying the request.

h3ct0rjs commented 6 years ago

Hi @sammchardy , I would like to use this issue to ask about the exceptions. What is the best way to get the exception failure name, I mean in a proper case we could have UNAUTH, Not found and others exceptions, so how you would catch the name of the exception or the specific point of failure. For example in the following code we have a try and except block, the idea is to print the exception name(UNAUTH, Not Found..etc)

try:
    client = ClientK(api_key.strip(), api_secret.strip())
    infouser = client.get_user()  #in order to test the api connection with the API
except KucoinAPIException:
     print('your exception was due to{}'.format(varexplanation) )

What would be the best option, because I would like to show a message with the failure but no all.

sammchardy commented 6 years ago

Hi @h3ct0rjs update your code like this so the exception is set as a variable

try:
    client = ClientK(api_key.strip(), api_secret.strip())
    infouser = client.get_user()  #in order to test the api connection with the API
except KucoinAPIException as e:
     print('your exception was due to{}'.format(e) )
     print('error code'.format(e.code) )  # UNAUTH
     print('error message'.format(e.message) )  # Not Found

The e variable is then of type KucoinAPIException and that has properties

h3ct0rjs commented 6 years ago

Thank you so much :)