Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace token generator script #142

Merged
merged 1 commit into from
Jun 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions bin/schwab-generate-token.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env python
import argparse
import atexit
import sys

import schwab

def main(api_key, app_secret, callback_url, token_path, requested_browser):
try:
schwab.auth.client_from_login_flow(
api_key, app_secret, callback_url, token_path,
requested_browser=requested_browser, callback_timeout=1)
return 0
except:
print('Failed to fetch a token using a web browser, falling back to '
'the manual flow')

schwab.auth.client_from_manual_flow(api_key, app_secret, callback_url,
token_path)

return 0


if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Fetch a new token and write it to a file')

required = parser.add_argument_group('required arguments')
required.add_argument(
'--token_file', required=True,
help='Path to token file. Any existing file will be overwritten')
required.add_argument('--api_key', required=True)
required.add_argument('--app_secret', required=True)
required.add_argument('--callback_url', required=True, type=str)
required.add_argument(
'--browser', required=False, type=str,
help='Manually specify a browser in which to start the login '+
'flow. See here for available options: '+
'https://docs.python.org/3/library/webbrowser.html#webbrowser.register')

args = parser.parse_args()

sys.exit(main(args.api_key, args.app_secret, args.callback_url,
args.token_file, args.browser))
17 changes: 8 additions & 9 deletions docs/auth.rst
Original file line number Diff line number Diff line change
Expand Up @@ -110,23 +110,22 @@ and use it on another.

.. code-block:: bash

# Notice we don't prefix this with "python" because this is a script that was
# installed by pip when you installed schwab-py
> schwab-generate-token.py --help
usage: schwab-generate-token.py [-h] --token_file TOKEN_FILE --api_key API_KEY
--redirect_uri REDIRECT_URI
usage: schwab-generate-token.py [-h] --token_file TOKEN_FILE --api_key API_KEY --app_secret APP_SECRET --callback_url CALLBACK_URL [--browser BROWSER]

Fetch a new token and write it to a file

optional arguments:
options:
-h, --help show this help message and exit

required arguments:
--token_file TOKEN_FILE
Path to token file. Any existing file will be overwritten
Path to token file. Any existing file will be overwritten
--api_key API_KEY
--redirect_uri REDIRECT_URI

--app_secret APP_SECRET
--callback_url CALLBACK_URL
--browser BROWSER Manually specify a browser in which to start the login flow. See here for available options:
https://docs.python.org/3/library/webbrowser.html#webbrowser.register


This script is installed by ``pip``, and will only be accessible if you've added
pip's executable locations to your ``$PATH``. If you're having a hard time, feel
Expand Down
1 change: 0 additions & 1 deletion schwab/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,6 @@ def callback_server():
'this method with interactive=False to skip this input.')

controller = webbrowser.get(requested_browser)
print(webbrowser.get)
controller.open(authorization_url)

# Wait for a response
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
license='MIT',
scripts=[
'bin/schwab-order-codegen.py',
'bin/schwab-generate-token.py',
],
)

Loading