Skip to content

Commit a311add

Browse files
committed
replace token generator script
1 parent d14175a commit a311add

File tree

4 files changed

+53
-10
lines changed

4 files changed

+53
-10
lines changed

bin/schwab-generate-token.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env python
2+
import argparse
3+
import atexit
4+
import sys
5+
6+
import schwab
7+
8+
def main(api_key, app_secret, callback_url, token_path, requested_browser):
9+
try:
10+
schwab.auth.client_from_login_flow(
11+
api_key, app_secret, callback_url, token_path,
12+
requested_browser=requested_browser, callback_timeout=1)
13+
return 0
14+
except:
15+
print('Failed to fetch a token using a web browser, falling back to '
16+
'the manual flow')
17+
18+
schwab.auth.client_from_manual_flow(api_key, app_secret, callback_url,
19+
token_path)
20+
21+
return 0
22+
23+
24+
if __name__ == '__main__':
25+
parser = argparse.ArgumentParser(
26+
description='Fetch a new token and write it to a file')
27+
28+
required = parser.add_argument_group('required arguments')
29+
required.add_argument(
30+
'--token_file', required=True,
31+
help='Path to token file. Any existing file will be overwritten')
32+
required.add_argument('--api_key', required=True)
33+
required.add_argument('--app_secret', required=True)
34+
required.add_argument('--callback_url', required=True, type=str)
35+
required.add_argument(
36+
'--browser', required=False, type=str,
37+
help='Manually specify a browser in which to start the login '+
38+
'flow. See here for available options: '+
39+
'https://docs.python.org/3/library/webbrowser.html#webbrowser.register')
40+
41+
args = parser.parse_args()
42+
43+
sys.exit(main(args.api_key, args.app_secret, args.callback_url,
44+
args.token_file, args.browser))

docs/auth.rst

+8-9
Original file line numberDiff line numberDiff line change
@@ -110,23 +110,22 @@ and use it on another.
110110

111111
.. code-block:: bash
112112
113-
# Notice we don't prefix this with "python" because this is a script that was
114-
# installed by pip when you installed schwab-py
115-
> schwab-generate-token.py --help
116-
usage: schwab-generate-token.py [-h] --token_file TOKEN_FILE --api_key API_KEY
117-
--redirect_uri REDIRECT_URI
113+
usage: schwab-generate-token.py [-h] --token_file TOKEN_FILE --api_key API_KEY --app_secret APP_SECRET --callback_url CALLBACK_URL [--browser BROWSER]
118114
119115
Fetch a new token and write it to a file
120116
121-
optional arguments:
117+
options:
122118
-h, --help show this help message and exit
123119
124120
required arguments:
125121
--token_file TOKEN_FILE
126-
Path to token file. Any existing file will be overwritten
122+
Path to token file. Any existing file will be overwritten
127123
--api_key API_KEY
128-
--redirect_uri REDIRECT_URI
129-
124+
--app_secret APP_SECRET
125+
--callback_url CALLBACK_URL
126+
--browser BROWSER Manually specify a browser in which to start the login flow. See here for available options:
127+
https://docs.python.org/3/library/webbrowser.html#webbrowser.register
128+
130129
131130
This script is installed by ``pip``, and will only be accessible if you've added
132131
pip's executable locations to your ``$PATH``. If you're having a hard time, feel

schwab/auth.py

-1
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,6 @@ def callback_server():
388388
'this method with interactive=False to skip this input.')
389389

390390
controller = webbrowser.get(requested_browser)
391-
print(webbrowser.get)
392391
controller.open(authorization_url)
393392

394393
# Wait for a response

setup.py

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
license='MIT',
6565
scripts=[
6666
'bin/schwab-order-codegen.py',
67+
'bin/schwab-generate-token.py',
6768
],
6869
)
6970

0 commit comments

Comments
 (0)