alpacahq / Alpaca-API

The Alpaca API is a developer interface for trading operations and market data reception through the Alpaca platform.
https://alpaca.markets/
141 stars 13 forks source link

SSL Certificate Verify Failed: certificate has expired (_ssl.c:1076)'))) #221

Open hriday2771 opened 2 years ago

hriday2771 commented 2 years ago

I updated the alpaca-trade-api python package from version 1.1.1 to version 1.5.1 and I have been getting an error trying to use the API since.

The error is as follows:

SSLCertVerificationError Traceback (most recent call last) ~\AppData\Roaming\Python\Python37\site-packages\urllib3\connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw) 709 headers=headers, --> 710 chunked=chunked, 711 )

~\AppData\Roaming\Python\Python37\site-packages\urllib3\connectionpool.py in _make_request(self, conn, method, url, timeout, chunked, **httplib_request_kw) 385 try: --> 386 self._validate_conn(conn) 387 except (SocketTimeout, BaseSSLError) as e:

~\AppData\Roaming\Python\Python37\site-packages\urllib3\connectionpool.py in _validate_conn(self, conn) 1039 if not getattr(conn, "sock", None): # AppEngine might not have .sock -> 1040 conn.connect() 1041

~\AppData\Roaming\Python\Python37\site-packages\urllib3\connection.py in connect(self) 425 ssl_context=context, --> 426 tls_in_tls=tls_in_tls, 427 )

~\AppData\Roaming\Python\Python37\site-packages\urllib3\util\ssl_.py in ssl_wrap_socket(sock, keyfile, certfile, cert_reqs, ca_certs, server_hostname, ssl_version, ciphers, ssl_context, ca_cert_dir, key_password, ca_cert_data, tls_in_tls) 449 ssl_sock = _ssl_wrap_socket_impl( --> 450 sock, context, tls_in_tls, server_hostname=server_hostname 451 )

~\AppData\Roaming\Python\Python37\site-packages\urllib3\util\ssl_.py in _ssl_wrap_socket_impl(sock, ssl_context, tls_in_tls, server_hostname) 492 if server_hostname: --> 493 return ssl_context.wrap_socket(sock, server_hostname=server_hostname) 494 else:

~\Anaconda3\lib\ssl.py in wrap_socket(self, sock, server_side, do_handshake_on_connect, suppress_ragged_eofs, server_hostname, session) 422 context=self, --> 423 session=session 424 )

~\Anaconda3\lib\ssl.py in _create(cls, sock, server_side, do_handshake_on_connect, suppress_ragged_eofs, server_hostname, context, session) 869 raise ValueError("do_handshake_on_connect should not be specified for non-blocking sockets") --> 870 self.do_handshake() 871 except (OSError, ValueError):

~\Anaconda3\lib\ssl.py in do_handshake(self, block) 1138 self.settimeout(None) -> 1139 self._sslobj.do_handshake() 1140 finally:

SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1076)

During handling of the above exception, another exception occurred:

MaxRetryError Traceback (most recent call last) ~\Anaconda3\lib\site-packages\requests\adapters.py in send(self, request, stream, timeout, verify, cert, proxies) 449 retries=self.max_retries, --> 450 timeout=timeout 451 )

~\AppData\Roaming\Python\Python37\site-packages\urllib3\connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw) 785 retries = retries.increment( --> 786 method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2] 787 )

~\AppData\Roaming\Python\Python37\site-packages\urllib3\util\retry.py in increment(self, method, url, response, error, _pool, _stacktrace) 591 if new_retry.is_exhausted(): --> 592 raise MaxRetryError(_pool, url, error or ResponseError(cause)) 593

MaxRetryError: HTTPSConnectionPool(host='paper-api.alpaca.markets', port=443): Max retries exceeded with url: /v2/account (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1076)')))

During handling of the above exception, another exception occurred:

SSLError Traceback (most recent call last)

in 28 api_secret = accounts[key][1] 29 api = tradeapi.REST(api_key, api_secret, base_url, api_version='v2') ---> 30 account = api.get_account() 31 capitals.append(pd.to_numeric(account.portfolio_value)) ~\AppData\Roaming\Python\Python37\site-packages\alpaca_trade_api\rest.py in get_account(self) 259 def get_account(self) -> Account: 260 """Get the account""" --> 261 resp = self.get('/account') 262 return self.response_wrapper(resp, Account) 263 ~\AppData\Roaming\Python\Python37\site-packages\alpaca_trade_api\rest.py in get(self, path, data) 237 238 def get(self, path, data=None): --> 239 return self._request('GET', path, data) 240 241 def post(self, path, data=None): ~\AppData\Roaming\Python\Python37\site-packages\alpaca_trade_api\rest.py in _request(self, method, path, data, base_url, api_version) 200 while retry >= 0: 201 try: --> 202 return self._one_request(method, url, opts, retry) 203 except RetryException: 204 retry_wait = self._retry_wait ~\AppData\Roaming\Python\Python37\site-packages\alpaca_trade_api\rest.py in _one_request(self, method, url, opts, retry) 219 """ 220 retry_codes = self._retry_codes --> 221 resp = self._session.request(method, url, **opts) 222 try: 223 resp.raise_for_status() ~\Anaconda3\lib\site-packages\requests\sessions.py in request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json) 527 } 528 send_kwargs.update(settings) --> 529 resp = self.send(prep, **send_kwargs) 530 531 return resp ~\Anaconda3\lib\site-packages\requests\sessions.py in send(self, request, **kwargs) 643 644 # Send the request --> 645 r = adapter.send(request, **kwargs) 646 647 # Total elapsed time of the request (approximately) ~\Anaconda3\lib\site-packages\requests\adapters.py in send(self, request, stream, timeout, verify, cert, proxies) 515 if isinstance(e.reason, _SSLError): 516 # This branch is for urllib3 v1.22 and later. --> 517 raise SSLError(e, request=request) 518 519 raise ConnectionError(e, request=request) SSLError: HTTPSConnectionPool(host='paper-api.alpaca.markets', port=443): Max retries exceeded with url: /v2/account (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1076)'))) The code I am using that is giving me the error is the get_account() method. api = tradeapi.REST(api_key, api_secret, base_url, api_version='v2') account = api.get_account() The expected behavior is to be able to connect without any issues to the Alpaca API. From what I have read online so far, this seems to be a certificate expiry issue on Alpaca end but have not found any concrete solutions so far and hence posting here. Desktop: - OS: Windows 10 - Python version 3.7 - Alpaca Version: 1.5.1