Skip to content
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 SSL: CERTIFICATE_VERIFY_FAILED #882

Closed
scoshil opened this issue Dec 18, 2017 · 39 comments
Closed

Binance SSL: CERTIFICATE_VERIFY_FAILED #882

scoshil opened this issue Dec 18, 2017 · 39 comments
Assignees
Labels

Comments

@scoshil
Copy link

scoshil commented Dec 18, 2017

I'm trying to connect to binance, and just testing the load_markets function, but I'm getting an SSL error.

bin = ccxt.binance({'apiKey':BinanceKey1,'secret':BinanceSecret1})
bin.verbose = True
bin.load_markets()

And Output:

https://api.binance.com/api/v1/exchangeInfo GET https://api.binance.com/api/v1/exchangeInfo 
Request: {'User-Agent': 'ccxt/1.10.343 (+https://github.com/ccxt/ccxt) Python/3.6.3', 'Accept-Encoding': 'gzip, deflate'} None
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\urllib\request.py", line 1318, in do_open
    encode_chunked=req.has_header('Transfer-encoding'))
  File "C:\ProgramData\Anaconda3\lib\http\client.py", line 1239, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "C:\ProgramData\Anaconda3\lib\http\client.py", line 1285, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "C:\ProgramData\Anaconda3\lib\http\client.py", line 1234, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "C:\ProgramData\Anaconda3\lib\http\client.py", line 1026, in _send_output
    self.send(msg)
  File "C:\ProgramData\Anaconda3\lib\http\client.py", line 964, in send
    self.connect()
  File "C:\ProgramData\Anaconda3\lib\http\client.py", line 1400, in connect
    server_hostname=server_hostname)
  File "C:\ProgramData\Anaconda3\lib\ssl.py", line 407, in wrap_socket
    _context=self, _session=session)
  File "C:\ProgramData\Anaconda3\lib\ssl.py", line 814, in __init__
    self.do_handshake()
  File "C:\ProgramData\Anaconda3\lib\ssl.py", line 1068, in do_handshake
    self._sslobj.do_handshake()
  File "C:\ProgramData\Anaconda3\lib\ssl.py", line 689, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:777)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\site-packages\ccxt\base\exchange.py", line 319, in fetch
    response = opener.open(request, timeout=int(self.timeout / 1000))
  File "C:\ProgramData\Anaconda3\lib\urllib\request.py", line 526, in open
    response = self._open(req, data)
  File "C:\ProgramData\Anaconda3\lib\urllib\request.py", line 544, in _open
    '_open', req)
  File "C:\ProgramData\Anaconda3\lib\urllib\request.py", line 504, in _call_chain
    result = func(*args)
  File "C:\ProgramData\Anaconda3\lib\urllib\request.py", line 1361, in https_open
    context=self._context, check_hostname=self._check_hostname)
  File "C:\ProgramData\Anaconda3\lib\urllib\request.py", line 1320, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:777)>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/scoss/PycharmProjects/Bitcoin/ccxt_test.py", line 31, in <module>
    bin.load_markets()
  File "C:\ProgramData\Anaconda3\lib\site-packages\ccxt\base\exchange.py", line 786, in load_markets
    markets = self.fetch_markets()
  File "C:\ProgramData\Anaconda3\lib\site-packages\ccxt\binance.py", line 237, in fetch_markets
    response = self.publicGetExchangeInfo()
  File "C:\ProgramData\Anaconda3\lib\site-packages\ccxt\binance.py", line 669, in request
    response = self.fetch2(path, api, method, params, headers, body)
  File "C:\ProgramData\Anaconda3\lib\site-packages\ccxt\base\exchange.py", line 277, in fetch2
    return self.fetch(request['url'], request['method'], request['headers'], request['body'])
  File "C:\ProgramData\Anaconda3\lib\site-packages\ccxt\base\exchange.py", line 338, in fetch
    self.raise_error(ExchangeNotAvailable, url, method, e)
  File "C:\ProgramData\Anaconda3\lib\site-packages\ccxt\base\exchange.py", line 259, in raise_error
    details,
ccxt.base.errors.ExchangeNotAvailable: binance GET https://api.binance.com/api/v1/exchangeInfo <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:777)> 

Not having issues with other exchanges, and this key/secret pair works without ccxt

@kroitor kroitor self-assigned this Dec 18, 2017
@kroitor
Copy link
Member

kroitor commented Dec 18, 2017

Are you located in China?

@scoshil
Copy link
Author

scoshil commented Dec 18, 2017

I am located in Australia

@kroitor
Copy link
Member

kroitor commented Dec 18, 2017

@scoshil can you access it via a public CORS-proxy?

bin = ccxt.binance({
    'apiKey':BinanceKey1,
    'secret':BinanceSecret1,
    'proxy': 'https://cors-anywhere.herokuapp.com/',  # last slash critical
})
bin.verbose = True
bin.load_markets()

@kroitor
Copy link
Member

kroitor commented Dec 18, 2017

See also:

Looks like an issue with Anaconda Python certificates or root CA settings... Did you see any options regarding SSL certificates upon installing it? Can you verify that it has access to your system / OpenSSL certificates?

@kroitor
Copy link
Member

kroitor commented Dec 18, 2017

and this key/secret pair works without ccxt

Did you test that keypair with any other software? Can you clarify how exactly it "works" ?

@scoshil
Copy link
Author

scoshil commented Dec 18, 2017

I tested that pair with the lib at https://github.com/sammchardy/python-binance and it worked smoothly....haven't looked to see if that lib does anything special with proxies yet, but I didn't configure it any differently to normal.

I will look into those links and see if I can fix it this way.

cheers

@kroitor
Copy link
Member

kroitor commented Dec 18, 2017

@scoshil ok, I will check their sources on this issue, to see if there's a workaround for this. Thx! Will get back to you with a followup.

@kroitor
Copy link
Member

kroitor commented Dec 18, 2017

@scoshil can you paste here your code snippet that you used with https://github.com/sammchardy/python-binance for testing, please?

@scoshil
Copy link
Author

scoshil commented Dec 18, 2017

do you mean from python-binance?

Test code:

from binance.client import Client
client = Client(BinanceKey1,BinanceSecret1)
    order = client.create_order(symbol='BTCUSDT',side="SELL",type="LIMIT",price=21000,quantity=0.001,timeInForce='GTC')
    print(order)

Output:

{'symbol': 'BTCUSDT', 'orderId': 10690384, 'clientOrderId': 'w4N8ngpjd4lZvF2Pn8XfZa', 'transactTime': 1513577527632, 'price': '21000.00000000', 'origQty': '0.00100000', 'executedQty': '0.00000000', 'status': 'NEW', 'timeInForce': 'GTC', 'type': 'LIMIT', 'side': 'SELL'}

@kroitor
Copy link
Member

kroitor commented Dec 18, 2017

Yes, thanks...

Does this help with ccxt?

ContinuumIO/anaconda-issues#494 (comment)

@scoshil
Copy link
Author

scoshil commented Dec 18, 2017

@kroitor I tried that and it still gives me the same error unfortunately

@kroitor
Copy link
Member

kroitor commented Dec 18, 2017

@scoshil python-binance works because it uses the requests python package, and that in turn also links against non-default SSL certificates from certifi package. ccxt uses pure built-in HTTP and for some reason a particular installation of conda won't find its default SSL/CA certificates... If none of the above links and suggestions help, we may switch to requests as well. Can you find out where it stores certificates on your system?

@kroitor
Copy link
Member

kroitor commented Dec 18, 2017

I'd also suggest to try this: https://stackoverflow.com/questions/33699577/conda-update-failed-ssl-error-ssl-certificate-verify-failed-certificate-ver (or google for more "anaconda SSL" or "conda SSL", "anaconda ssl verify", etc...)

If nothing helps, I will do a clean install of Anaconda Python on a virtual Windows PC and will test/fix it then...

@kroitor
Copy link
Member

kroitor commented Dec 19, 2017

@scoshil can you, please, try reinstalling the anaconda python? (a fresh distrib from their site, if that's ok for you)... you would help us rule out one possible cause of it...

@scoshil
Copy link
Author

scoshil commented Dec 19, 2017

Thanks I'll try the things you mentioned and report back....may take me a little while to do a clean anaconda install though

@scoshil
Copy link
Author

scoshil commented Dec 19, 2017

Ok so I tried the following:

Really out of ideas at this point...

@kroitor
Copy link
Member

kroitor commented Dec 19, 2017

@scoshil ok, thanks so much for your effort and for reporting back! We will have to migrate to requests+certifi for their SSL certificates, I'll let you know on the progress, hopefully very soon.

@scoshil
Copy link
Author

scoshil commented Dec 19, 2017

Thanks!

@kroitor
Copy link
Member

kroitor commented Dec 19, 2017

@scoshil I can't reproduce the problem from my Windows 10 machine from Russia, I can access Binance normally...

screen shot 2017-12-19 at 10 09 02

As you see, a fresh install of Anaconda3 works on my side... So, maybe you have some certificates that are outdated or something... However, I don't know if it makes sense, because you did a clean reinstall...

After installing anaconda I found these two certificates on my setup C:\Users\myusername\AppData\local\Continuum\anaconda3\ssl:

cert.zip

You can try copying them into your ssl folder to see if they make any difference (save a backup copy of existing certificates beforehand). The fix is on the way...

@kroitor
Copy link
Member

kroitor commented Dec 19, 2017

Also, if you can, please run the following to see if the issue is indeed related to default SSL certificates:

import requests
print(requests.get('https://api.binance.com/api/v1/exchangeInfo').text)

Does the above snippet work?

@kroitor kroitor changed the title Binance SSL certificate failed Binance SSL: CERTIFICATE_VERIFY_FAILED Dec 19, 2017
@hexdump2002
Copy link

hexdump2002 commented Dec 19, 2017

I have executed what you suggested and this is the output:

{"timezone":"UTC","serverTime":1513694282583,"rateLimits":[{"rateLimitType":"REQUESTS","interval":"MINUTE","limit":1200},{"rateLimitType":"ORDERS","interval":"SECOND","limit":10},{"rateLimitType":"ORDERS","interval":"DAY","limit":100000}],"exchangeFilters":[],"symbols":[{"symbol":"ETHBTC","status":"TRADING","baseAsset":"ETH","baseAssetPrecision":8,"quoteAsset":"BTC","quotePrecision":8,"orderTypes":

........

{"filterType":"MIN_NOTIONAL","minNotional":"0.01000000"}]},{"symbol":"OSTBNB","status":"TRADING","baseAsset":"OST","baseAssetPrecision":8,"quoteAsset":"BNB","quotePrecision":8,"orderTypes":["LIMIT","LIMIT_MAKER","MARKET","STOP_LOSS_LIMIT","TAKE_PROFIT_LIMIT"],"icebergAllowed":true,"filters":[{"filterType":"PRICE_FILTER","minPrice":"0.00001000","maxPrice":"10000.00000000","tickSize":"0.00001000"},{"filterType":"LOT_SIZE","minQty":"0.01000000","maxQty":"10000000.00000000","stepSize":"0.01000000"},{"filterType":"MIN_NOTIONAL","minNotional":"1.00000000"}]}]}

It seems to work correctly. I'm the one that reported #904

@kroitor
Copy link
Member

kroitor commented Dec 19, 2017

@hexdump2002 thanks! That confirms my initial thought. Ok, the fix for this is coming very soon.

@hexdump2002
Copy link

Thanks a lot. We really need it because the library crashes in such basic thing.... By he way, your work with the lib is awesome. Thanks for sharing it!

@kroitor
Copy link
Member

kroitor commented Dec 19, 2017

@hexdump2002 thank you! Btw, just for completeness, if it's not a secret, what is your country/region/continent?

@hexdump2002
Copy link

Europe/Spain , I will provide anything you need :)

@hexdump2002
Copy link

@kroitor : With no intention of rushing you or anything, do you have any ETA for this bug to be fixed? We are at this stucked now :).

@kroitor
Copy link
Member

kroitor commented Dec 19, 2017

@hexdump2002 today or tomorrow.

@scoshil
Copy link
Author

scoshil commented Dec 20, 2017

@kroitor I can confirm I have the same output as @hexdump2002 for the requests code you provided

I also tried replacing my ssl certificates and it didn't change anything

@scoshil
Copy link
Author

scoshil commented Dec 20, 2017

@kroitor also just wanted to echo the thanks for sharing the lib! The work that's been done so far has been great!

@hexdump2002
Copy link

Following up the "switching to requests library" suggestions you commented yesterday. Will this affect the cctx.async part of the library? I mean, requests is not compatible with async, so we will have sync calls whenever we use async or not. Could you give a bit of information about this?

@kroitor
Copy link
Member

kroitor commented Dec 20, 2017

@hexdump2002

Will this affect the cctx.async part of the library?

No, if you mean breaking the async version, it will not affect the ccxt.async. But yes, if you mean switching the async to an async-compatible implementation of requests - it will affect the implementation only. The interface remains the same.

I mean, requests is not compatible with async

Yes, but we are not going to use the synchronous version of requests in our async subpackage, of course. We are going to integrate an async-aware implementation of requests, like requests-futures, or some other implementation that fits best for this job.

so we will have sync calls whenever we use async or not.

Nope, you will still have both sync and true-async modes available.

Hope it answers your question, let me know if you have more )

@hexdump2002
Copy link

Yes, I understand what you mean. So, the new fix that will come for this bug (The SSL one) will fix both versions? sync and async? Or you are fixing only the synched part only first?

@kroitor
Copy link
Member

kroitor commented Dec 20, 2017

So, the new fix that will come for this bug (The SSL one) will fix both versions? sync and async?

Yes, we will fix both of them, however, we are going to upload the sync-fix first, then the async-fix.

Or you are fixing only the synched part only first?

Yep, but there will be little to no delay between the sync-fix and the consequent async-fix. We are determined to fix them both now, however, we can't fix them both simultaneously (different underlying implementations), therefore the sync version is expected first, and then the async, second.

@kroitor
Copy link
Member

kroitor commented Dec 21, 2017

This issue is fixed in version 1.10.436. Can you guys test and verify it, please? We will be thankful for your reports!

@hexdump2002
Copy link

Ok, it seems to work now. If you need me to do any test let me know. Thanks a lot. I will be fully happy when you fix the async version too :D. Good Job!

Hopping to see it soon!

@aekramer
Copy link

@kroitor I am from issue #911
For as far as I can tell the issue seems to be fixed :)
Now it's time for me to get familair with your code and create some basic scripts.

Thank you for the quick support, and the big library

@aekramer
Copy link

@kroitor Will you let us know here when the async version is fixed?

Also a small note, not sure if the SSL issue is related to every exchange, however when running the example async-gdax-fetch-order-book-continously.py it seems to work with GDAX, but not when I change it to binance

@kroitor
Copy link
Member

kroitor commented Dec 21, 2017

@aekramer

Will you let us know here when the async version is fixed?

Sure, it is coming next.

it seems to work with GDAX, but not when I change it to binance

Will look into it, thx for reporting.

@scoshil
Copy link
Author

scoshil commented Dec 23, 2017

@kroitor tested fix and working as expected now! Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants