tylerebowers / Schwab-API-Python

An easy and lightweight wrapper for using the Charles Schwab API.
https://pypi.org/project/schwabdev/
MIT License
300 stars 117 forks source link

SSL: CERTIFICATE_VERIFY_FAILED - self-signed certificate in certificate chain error when connecting to streaming server #17

Closed dereknguyenio closed 5 months ago

dereknguyenio commented 5 months ago

SSL: CERTIFICATE_VERIFY_FAILED - self-signed certificate in certificate chain error when connecting to streaming server

Issue Description

Hello,

I'm encountering an SSL certificate verification issue when trying to connect to the streaming server using the Schwab API. Here are the details:

Environment

Code Snippet

import schwabdev
from dotenv import load_dotenv
import os

def main():
    client = schwabdev.Client(os.getenv('app_key'), os.getenv('app_secret'), os.getenv('callback_url'))
    client.update_tokens_auto()  # update tokens automatically (except refresh token)

    # example of using the default response handler
    client.stream.start()

    client.stream.send(client.stream.level_one_equities("AMD", "0,1,2,3,4,5,6,7,8"))

    # stop the stream after 60 seconds (since this is a demo)
    import time
    time.sleep(60)
    client.stream.stop()

if __name__ == '__main__':
    print("Welcome to the unofficial Schwab interface!\nGithub: https://github.com/tylerebowers/Schwab-API-Python")
    load_dotenv()
    main() 

Error Message

[INFO]: Refresh token last updated: 2024-06-21 10:29:02 (expires in 6 days)
[INFO]: Linked Accounts:
[INFO]: Initialization Complete
[INFO]: Connecting to streaming server -> [ERROR]: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self-signed certificate in certificate chain (_ssl.c:1000)
[ERROR]: Stream not alive for more than 1 minute, exiting...
[WARN]: Stream is not active, request queued.

Steps to Reproduce

Clone the repository. Set up the environment with the required API keys and secrets. Run the provided code snippet.

Could you please provide guidance on how to properly configure the SSL certificate validation or any workaround for this issue?

tylerebowers commented 5 months ago

Please check your system clock and make sure that your time is synced. You may have to install the pip packages pip-system-certs or certifi.

The issue is with your local certificates, we could not verify requests (this error would go away) but given the nature of data being sent it is important.

tylerebowers commented 5 months ago

Actually it appears that on MacOS you need to run a certificates installer. For you the command should be: open /Applications/Python\ 3.12/Install\ Certificates.command

dereknguyenio commented 5 months ago

Actually it appears that on MacOS you need to run a certificates installer. For you the command should be: open /Applications/Python\ 3.12/Install\ Certificates.command

This worked perfectly, thank you!