diff --git a/refresh_schwab_token.py b/refresh_schwab_token.py new file mode 100644 index 0000000..261f008 --- /dev/null +++ b/refresh_schwab_token.py @@ -0,0 +1,34 @@ +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) \ No newline at end of file diff --git a/token.json b/token.json new file mode 100644 index 0000000..2dc49d2 --- /dev/null +++ b/token.json @@ -0,0 +1 @@ +{"creation_timestamp": 1744667071, "token": {"expires_in": 1800, "token_type": "Bearer", "scope": "api", "refresh_token": "iZjtH8G_wElu5Iw-_IiP0ZIeiP2Qikmtji931m-UpcmuXZ4UFs-MKrqALNfMdpVb-ErVIDhnZLr78gL_I-LEAKGaVsBXSa-PeKyKC5UsXQTeyFGXjBmeKpLvu1X-E0yRvvVcnfqLO9M@", "access_token": "I0.b2F1dGgyLmNkYy5zY2h3YWIuY29t.rN7e5-o4kNvP5j9om0aQWJPrFJzcUZu0c8FHOSbjgD4@", "id_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiI2YmZkYzYyODMwMmY5YTQyNjYzYjM3MTM3ODcyN2IxZjM3ZDJkOTkyOWUwZjg3NDYzNjkwYzcyMThiZDA5MDllIiwiYXVkIjoidlV2c0RXTHR6V2tROHVmeExVTWhkd1Fpd0FDZ0dWM3UiLCJpc3MiOiJ1cm46Ly9hcGkuc2Nod2FiYXBpLmNvbSIsImV4cCI6MTc0NDY3MDY2OSwiaWF0IjoxNzQ0NjY3MDY5LCJqdGkiOiIwZTdhMDgxMi1lN2VmLTQ3OTctYTg4OC1jZjRiNDY5MjlmMzIifQ.gpTcd3-B0W92wxhXItyvI70WF7CTEyEznCdtPKROjcA", "expires_at": 1744668871}} \ No newline at end of file