-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGenerate_AuthCode.py
63 lines (53 loc) · 2.41 KB
/
Generate_AuthCode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import http.server
import socketserver
import requests
import webbrowser
"""
Get Auth0 auth-code through login from TradeStation.
This code will later be used to generate refresh_code.
"""
# Replace the key/ client_id in the credentials.txt file.
with open('credentials.txt', 'r') as f:
AUTH0_CLIENT_ID = f.readline().strip().split(': ')[-1]
print(AUTH0_CLIENT_ID)
# Replace AUTH0_SCOPE if required.
AUTH0_SCOPE = 'openid offline_access profile MarketData ReadAccount Trade Crypto Matrix OptionSpreads'
# Change the redirect_uri with any another port on localhost if required or set custom redirect_uri that you
# requested from TradeStation. Read more about redirect_uri and allowed default redirect_uri.
REDIRECT_URI = 'http://localhost:3000/'
# Generate the authorization URL
url = ('https://signin.tradestation.com/authorize?response_type=code&client_id={'
'}&audience=https://api.tradestation.com&redirect_uri={}&scope={}').format(AUTH0_CLIENT_ID, REDIRECT_URI,
AUTH0_SCOPE)
print("URL for authorization: "+url)
# Open the authorization URL in Chrome
webbrowser.open_new(url)
# Start the HTTP server to listen for the callback
class CallbackHandler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
print("Response Handeler")
print(self.path)
code = self.path.split('=')[-1]
# Save the authorization code to a text file
with open('auth_code.txt', 'w') as f:
f.write(code)
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bytes('<html><body>Authorization code saved to file. This window can be closed.</body></html>', 'utf-8'))
self.server.server_close()
print("Authorization code aquired. Server connection closed")
print("try socket server")
try:
with socketserver.TCPServer(("", 3000), CallbackHandler) as httpd:
httpd.serve_forever()
except Exception as e:
print("failed to run local server for authorization code: ")
print(e)
try:
httpd.server_close()# provents error in which a server from a last attempt is still running
print("port closed")
except:
print("Could not shut down web server. If it remains open there is likely to be errors when the script is run.")
exit()
print("Authorization code saved to file.")