itsjafer / schwab-api

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

Add handling of different Bearer tokens #29

Closed WinesProof closed 8 months ago

WinesProof commented 8 months ago

The Schwab web interface uses different Bearer tokens for different endpoints. It isn't clear why things worked some/most of the time without this change, but hopefully things will work more reliably now, especially #22 .

cnMuggle commented 8 months ago

some error codes I see when the original update_token() is applied. I initially comment the update_token() and only try to apply update_token() when r.status_code != 200. But it still will result in the below error.

image image

I will try the changes in this pull and see if it helps.

WinesProof commented 8 months ago

Interesting. I think the first error image you posted occurred when converting the return text into a json json.loads(r.text), so the attempt to get the token failed for some reason. We'd need to print out the error message for that in order to figure out why.

The second error image you posted is because the cookie jar sometimes is like a dict, but it really isn't, and it can have two items with the same name. A better way to delete all cookies of a specified name is to use requests.cookies.remove_cookie_by_name(self.session.cookies, 'ADRUM_BT1').

However, after looking more at the website behavior in my browser, no cookies are included in the v2 api requests, so perhaps a better way to do this is to use requests.get or requests.post instead of self.session.get or self.session.post. I'll try that for my personal use for a bit to see how well it works.

That said, I'm getting the impression that @cnMuggle and others may have long-running scripts (or ones that do a lot more than mine before finishing) that may encounter different issues than I typically see. So I may not be able to troubleshoot all those.

All that to say, I'm going to close this PR and look at cookie/token handling more before proposing a better way.

cnMuggle commented 8 months ago

image image