from schwab import auth, client from authlib.integrations.base_client.errors import OAuthError import os import json import datetime script_directory = os.path.dirname(os.path.abspath(__file__)) api_key = 'mwxx0yUewlCWbRscYEhArMVoJiS8GTGP' app_secret = 'C41aiGaJUFgdMnTu' redirect_uri = 'https://127.0.0.1:8182' token_path = os.path.join(script_directory, 'token.json') try: c = auth.client_from_token_file(token_path, api_key, app_secret) except FileNotFoundError as e: print(e) c = auth.client_from_login_flow( api_key, app_secret, redirect_uri, token_path) try: r = c.get_price_history("TSLA", period_type=client.Client.PriceHistory.PeriodType.DAY, start_datetime=datetime.datetime.strptime("2024-8-1", "%Y-%m-%d"), end_datetime=datetime.datetime.strptime("2024-8-1", "%Y-%m-%d"), frequency_type=client.Client.PriceHistory.FrequencyType.MINUTE, frequency=client.Client.PriceHistory.Frequency.EVERY_THIRTY_MINUTES) # assert r.status_code == 200, r.raise_for_status() print(json.dumps(r.json(), indent=4)) except OAuthError: print("Token is invalid; please refresh manually") auth.client_from_login_flow(api_key, app_secret, redirect_uri, token_path)