Created
May 15, 2024 17:09
-
-
Save hn4002/4616de7004fb9306814fe8236c27382e to your computer and use it in GitHub Desktop.
schwab-py manual auth with some meta info
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # This script is used to manually authenticate the user with the Schwab API. | |
| # It deletes the token file at the beginning. It also creates a meta file with the current timestamp. | |
| import datetime | |
| import json | |
| import os | |
| import pathlib | |
| import pytz | |
| import sys | |
| import schwab | |
| from environment.instance_settings import schwabSettings | |
| client_id = schwabSettings.SCHWAB_APP_ID | |
| client_secret = schwabSettings.SCHWAB_APP_SECRET | |
| redirect_uri = schwabSettings.SCHWAB_REDIRECT_URI | |
| token_path = schwabSettings.SCHWAB_TOKEN_PATH | |
| #=============================================================================== | |
| def manual_auth(): | |
| # First delete the token file if it exists | |
| if os.path.exists(token_path): | |
| os.remove(token_path) | |
| client = schwab.auth.client_from_manual_flow( | |
| api_key=client_id, | |
| app_secret=client_secret, | |
| callback_url=redirect_uri, | |
| token_path=token_path | |
| ) | |
| # Create a file to save the timestamp for the last time the refresh token was generated | |
| meta_path = pathlib.Path(token_path).with_suffix(".meta.json") | |
| # First delete the meta file if it exists | |
| if os.path.exists(meta_path): | |
| os.remove(meta_path) | |
| # Now create the meta file with the current timestamp | |
| curr_dt = datetime.datetime.now(pytz.timezone('US/Eastern')) | |
| data = { | |
| 'last_refresh_time': curr_dt.isoformat() | |
| } | |
| with open(meta_path, "w") as f: | |
| f.write(json.dumps(data, indent=4)) | |
| #=============================================================================== | |
| if __name__ == '__main__': | |
| manual_auth() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment