Compare commits

...

2 Commits

Author SHA1 Message Date
brld cc8708e2c9 Add schwab token and refresh method 2025-04-18 15:01:10 -07:00
brld 7f912f68cf Add gitignore 2025-03-20 16:06:59 -07:00
3 changed files with 36 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.venv/

34
refresh_schwab_token.py Normal file
View File

@ -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)

1
token.json Normal file
View File

@ -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}}