alexgolec / tda-api

A TD Ameritrade API client for Python. Includes historical data for equities and ETFs, options chains, streaming order book data, complex order construction, and more.
https://tda-api.readthedocs.io
MIT License
1.26k stars 335 forks source link

Access token not refreshing #349

Open bman393 opened 3 months ago

bman393 commented 3 months ago

Description of Bug I created my token with client_from_manual_flow, all my inputs are correct. It then only gives me a expires_in token of 1800 (30 min).

Then I use schwab.auth.client_from_token_file to create my client which is also suppose to refresh my token as well right? But my token still expires on the first set 30 min

My token file exists on the same level as the code I am using to grab quotes. Here is my code:

`from schwab.auth import client_from_manual_flow

api_key='', app_secret='', callback_url='https://127.0.0.1', token_path='/mnt/c/Users/Brogan/Documents/tickerStats/schwab-py/token.json'

def getPrice(ticker):

client = schwab.auth.client_from_token_file(token_path, api_key, app_secret)

r = client.get_quote(ticker)

r=r.json()
print(r)

getPrice('$SPX')`

Expected Behavior I expect the access token to update to a new expire time.

Actual Behavior Instead, the token still expires at my original set time from the manual token creation. I have to manually create the token every 30 min to access schwab

Error/Exception Log, If Applicable log.txt

bman393 commented 3 months ago

Adding log output from failed refresh log2.txt

arsaboo commented 3 months ago

Please add the logs (instead of attaching them) in a code block.

bman393 commented 3 months ago

Passing client fetch log:

`schwab-api version 0.0.0a29 Loading token from file /mnt/c/Users/Brogan/Documents/tickerStats/schwab-py/token.json Req 1: GET to https://api.schwabapi.com/marketdata/v1/$SPX/quotes, params={} Req 1: GET response: 200, content={"$SPX":{"assetMainType":"INDEX","realtime":true,"ssid":1819771877,"symbol":"$SPX","fundamental":{"avg10DaysVolume":0.0,"avg1YearVolume":0.0,"divAmount":0.0,"divFreq":0,"divPayAmount":0.0,"divYield":0.0,"eps":0.0,"fundLeverageFactor":0.0,"peRatio":0.0},"quote":{"52WeekHigh":5325.49,"52WeekLow":4103.78,"closePrice":5308.13,"highPrice":5324.32,"lastPrice":5321.41,"lowPrice":5297.87,"netChange":13.28,"netPercentChange":0.25018227,"openPrice":5298.69,"securityStatus":"Closed","totalVolume":1974738504,"tradeTime":1716325754901},"reference":{"description":"S\u0026P 500 INDEX","exchange":"0","exchangeName":"Index"}}} {'$SPX': {'assetMainType': 'INDEX', 'realtime': True, 'ssid': 1819771877, 'symbol': '$SPX', 'fundamental': {'avg10DaysVolume': 0.0, 'avg1YearVolume': 0.0, 'divAmount': 0.0, 'divFreq': 0, 'divPayAmount': 0.0, 'divYield': 0.0, 'eps': 0.0, 'fundLeverageFactor': 0.0, 'peRatio': 0.0}, 'quote': {'52WeekHigh': 5325.49, '52WeekLow': 4103.78, 'closePrice': 5308.13, 'highPrice': 5324.32, 'lastPrice': 5321.41, 'lowPrice': 5297.87, 'netChange': 13.28, 'netPercentChange': 0.25018227, 'openPrice': 5298.69, 'securityStatus': 'Closed', 'totalVolume': 1974738504, 'tradeTime': 1716325754901}, 'reference': {'description': 'S&P 500 INDEX', 'exchange': '0', 'exchangeName': 'Index'}}}

BEGIN REDACTED LOGS

[debug.py:151:_enable_bug_report_logging] schwab-api version 0.0.0a29 [auth.py:36:load_token] Loading token from file /mnt/c/Users/Brogan/Documents/tickerStats/schwab-py/token.json [synchronous.py:17:_get_request] Req 1: GET to https://api.schwabapi.com/marketdata/v1/$SPX/quotes, params={} [base.py:64:_log_response] Req 1: GET response: 200, content={"$SPX":{"assetMainType":"INDEX","realtime":true,"ssid":1819771877,"symbol":"$SPX","fundamental":{"avg10DaysVolume":0.0,"avg1YearVolume":0.0,"divAmount":0.0,"divFreq":0,"divPayAmount":0.0,"divYield":0.0,"eps":0.0,"fundLeverageFactor":0.0,"peRatio":0.0},"quote":{"52WeekHigh":5325.49,"52WeekLow":4103.78,"closePrice":5308.13,"highPrice":5324.32,"lastPrice":5321.41,"lowPrice":5297.87,"netChange":13.28,"netPercentChange":0.25018227,"openPrice":5298.69,"securityStatus":"Closed","totalVolume":1974738504,"tradeTime":1716325754901},"reference":{"description":"S\u0026P 500 INDEX","exchange":"0","exchangeName":"Index"}}}`

Failing client fetch log:

`schwab-api version 0.0.0a29 Loading token from file token.json Req 1: GET to https://api.schwabapi.com/marketdata/v1/$SPX/quotes, params={} Traceback (most recent call last): File "get_quote.py", line 27, in getPrice('$SPX') File "get_quote.py", line 23, in getPrice r = client.get_quote(ticker) File "/mnt/c/Users/Brogan/Documents/tickerStats/schwab-py/schwab/client/base.py", line 456, in get_quote return self._get_request(path, params) File "/mnt/c/Users/Brogan/Documents/tickerStats/schwab-py/schwab/client/synchronous.py", line 19, in _get_request resp = self.session.get(dest, params=params) File "/home/brmcgraw/.local/lib/python3.6/site-packages/httpx/_client.py", line 983, in get timeout=timeout, File "/home/brmcgraw/.local/lib/python3.6/site-packages/authlib/integrations/httpx_client/oauth2_client.py", line 196, in request self.ensure_active_token() File "/home/brmcgraw/.local/lib/python3.6/site-packages/authlib/integrations/httpx_client/oauth2_client.py", line 207, in ensure_active_token self.refresh_token(url, refresh_token=refresh_token) File "/home/brmcgraw/.local/lib/python3.6/site-packages/authlib/oauth2/client.py", line 264, in refresh_token auth=auth, **session_kwargs) File "/home/brmcgraw/.local/lib/python3.6/site-packages/authlib/oauth2/client.py", line 275, in _refresh_token token = self.parse_response_token(resp.json()) File "/home/brmcgraw/.local/lib/python3.6/site-packages/authlib/oauth2/client.py", line 380, in parse_response_token self.handle_error(error, description) File "/home/brmcgraw/.local/lib/python3.6/site-packages/authlib/integrations/httpx_client/oauth2_client.py", line 188, in handle_error raise OAuthError(error_type, error_description) authlib.integrations.base_client.errors.OAuthError: invalid_client: Unauthorized

BEGIN REDACTED LOGS

[debug.py:151:_enable_bug_report_logging] schwab-api version 0.0.0a29 [auth.py:36:load_token] Loading token from file token.json [synchronous.py:17:_get_request] Req 1: GET to https://api.schwabapi.com/marketdata/v1/$SPX/quotes, params={}`

alexgolec commented 3 months ago

Wait a second, this is the wrong repo... Can you re-enter your ticket on the new library? https://github.com/alexgolec/schwab-py/issues

bman393 commented 3 months ago

oh shoot, yes let me do that