Repository navigation
Expand file tree
/
Copy pathauth.py
More file actions
49 lines (40 loc) · 1.96 KB
/
Copy pathauth.py
File metadata and controls
49 lines (40 loc) · 1.96 KB
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
from http.server import HTTPServer, BaseHTTPRequestHandler
from urllib.parse import parse_qs
import requests
import ssl
class Handler(BaseHTTPRequestHandler):
def _set_headers(self):
self.send_response(200)
self.send_header('Content-Type', 'application/json')
self.end_headers()
def do_GET(self):
self._set_headers()
#Get the Auth Code
path, _, query_string = self.path.partition('?')
code = parse_qs(query_string)['code'][0]
#code = input("Copy/Paste code here:\n")
print(code)
#Post Access Token Request
headers = { 'Content-Type': 'application/x-www-form-urlencoded' }
data = { 'grant_type': 'authorization_code', 'access_type': 'offline', 'code': code, 'client_id': 'AWESOMECAP1', 'redirect_uri': 'https://127.0.0.1'}
authReply = requests.post('https://api.tdameritrade.com/v1/oauth2/token', headers=headers, data=data)
#returned just to test that it's working
self.wfile.write(authReply.text.encode())
raw = authReply.json()
print ("\n\n*** WROTE ACCESS/REFRESH TOKEN TO FILE ***\n\n")
access_token = raw['access_token']
refresh_token = raw['refresh_token']
access_token_path = 'access_token'
refresh_token_path = 'refresh_token'
access_token_file = open('access_token','w')
refresh_token_file= open('refresh_token','w')
access_token_file.write(access_token)
refresh_token_file.write(refresh_token)
httpd = HTTPServer(('127.0.0.1', 443), Handler)
#SSL cert
httpd.socket = ssl.wrap_socket (httpd.socket,
keyfile='key.pem',
certfile='certificate.pem', server_side=True)
#CHANGE "AWESOMECAP1" and your redirect URL if needed
print ("POINT BROWSER TO: https://auth.tdameritrade.com/auth?response_type=code&redirect_uri=https://127.0.0.1&client_id=AWESOMECAP1%40AMER.OAUTHAP")
httpd.serve_forever()