itsjafer / schwab-api

A python library for placing trades on Charles Schwab
MIT License
201 stars 64 forks source link

Match v2 requests to website #36

Closed WinesProof closed 8 months ago

WinesProof commented 8 months ago

When using a web browser, v2 requests (all the ausgateway.schwab.com urls) do not include cookies and use at least two different bearer tokens depending on the endpoint. This updates the v2 requests to match that.

itsjafer commented 8 months ago

Thanks for the investigation and work here! I'll be ready to revert if anyone reports regression, but this LGTM!

cnMuggle commented 8 months ago

Thanks, It looks great to me as well. I will try this when I have time

cnMuggle commented 8 months ago

I have below exception triggered and hence noticed the row 350 missed a "f" image it can be updated to raise ValueError(f"Error updating Bearer token: {r.reason}")

image everything else so far so good. Thanks.

WinesProof commented 8 months ago

Good catch. See #39

But if you got that exception, it means something went wrong when attempting to update the token, so we'll eventually want to track that down. The fix for that error message will hopefully help reveal what that is.

cnMuggle commented 8 months ago

Thanks for your update.

Sorry if I have said this twice. The update_token used elsewhere sometimes will also trigger exception. My current work-around is to catch this exception and use another login() to bypass. Doing this at least on my side is stable/reliable for a while loop to fetch data all day long. Call update_token more than once at the same place as what itsjafer has tried (in the commented code) doesn't work on my test too.

WinesProof commented 8 months ago

Ah, yes, if you're trying to keep something running for a long time that doesn't surprise me. How long does it usually take to get that error?

cnMuggle commented 8 months ago

I update the market data every 5 minutes, I think it just expired due to no-activity. image

WinesProof commented 8 months ago

Using this code, it looks like it logs out after an hour regardless of activity (or at least I haven't tried whatever activity might keep it logged in). Using a browser, it logs out after an hour unless you click on a link or refresh the page. Perhaps there's some way to spoof (ideally without playwright) whatever happens when clicking/refreshing.

cnMuggle commented 8 months ago

I am satisfied with what we have right now. The other platform API is more difficult to use and need regularly human interaction due to security concern. The schwab API is the most convenient so far. Just catch the exception and re-login seems so easy from my point of view.