alexgolec / schwab-py

Unofficial API wrapper for the Schwab HTTP API
MIT License
184 stars 44 forks source link

Login error when using client_from_login_flow #149

Closed felonfusk closed 2 months ago

felonfusk commented 2 months ago

Please read the bug submission guidelines before submitting a bug.

Not following guidelines may result in your bug being ignored and/or closed.

Description of Bug Trying to use client_from_login_flow to get a new token. But received error at Schwab login. error

My credential is correct, as I was able to login and create new tokens without using client_from_login_flow The same error happens to client_from_manual_flow as well.

python: 3.11 schwab-py: 1.2.1

Code to Reproduce

import schwab
api_key = ""
app_secret = ""
callback_url = "https://127.0.0.1:8182"
token_path = 'token.json'

def login_works():
    import webbrowser
    auth_url = (f"https://api.schwabapi.com/v1/oauth/authorize"
                   f"?client_id={api_key}&redirect_uri=https://127.0.0.1")
    webbrowser.open(auth_url)

def login_error():
    schwab.auth.client_from_login_flow(api_key, app_secret, callback_url, token_path)

Expected Behavior Log into Schwab's account page

Actual Behavior Error at Schwab login screen

Error/Exception Log, If Applicable See here to learn how to turn on debug logging: https://tda-api.readthedocs.io/en/latest/help.html

alexgolec commented 2 months ago

I can't help but notice that you're sending two different callback URLs there. The one in login_works sends https://127.0.0.1 whereas the one in login_error sends https://127.0.0.1:8182. Are you sure you're not setting the wrong callback URL? I've encountered this same error in the past and was similarly confused.

felonfusk commented 2 months ago

I can't help but notice that you're sending two different callback URLs there. The one in login_works sends https://127.0.0.1 whereas the one in login_error sends https://127.0.0.1:8182. Are you sure you're not setting the wrong callback URL? I've encountered this same error in the past and was similarly confused.

I think my https://127.0.0.1:8182 works, as I was able to call client_from_manual_flow in the issue . And it wouldn't allow me to use the callback url without specifying the port or it will give me an error.

...schwab.auth.RedirectServerExitedError: Redirect server exited. Are you attempting to use a callback URL without a port number specified?
alexgolec commented 2 months ago

Are both https://127.0.0.1:8182 and https://127.0.0.1 configured as callback URLs for your application? The situation you describe is exactly what happens if you attempt to pass anything that isn't a configured callback.

felonfusk commented 2 months ago

You are right. My app settings show the callback url is just https://127.0.0.1, without the port number. I just created a 2nd app with https://127.0.0.1:8182 this time, will report back once it's approved.

alexgolec commented 2 months ago

Working as intended, marking as won't fix.

As an FYI, I haven't had any success getting a second app approved, you may find it goes into the Approved - Pending state forever. I recommend updating your existing app to either modify the existing callback URL to include a port number, or adding a new callback URL alongside the existing one. If the only thing that's using your callback URL is schwab-py, I recommend the first option.