-
Notifications
You must be signed in to change notification settings - Fork 0
/
Authentication.py
59 lines (44 loc) · 2.1 KB
/
Authentication.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
from splinter import Browser
import requests
import urllib.parse
from auth_info import consumer_key,redirect_uri,client_id,username,password
#Create a new chrome browser
executable_path = {'executable_path':r'C:/Coding_Practice/chromedriver'}
browser=Browser('chrome',**executable_path,headless=False)
#Information needed to authenticate my TD account
auth_url='https://auth.tdameritrade.com/auth'
#Params pass into authenticate url in order to obtain a code
params_temp={'response_type' : 'code',
'redirect_uri' : redirect_uri,
'client_id' : client_id}
params=urllib.parse.urlencode(params_temp)
#Newly created url that asks users to authenticate themselves
authorization_url = auth_url + '?' + params
#Visit the website with the browser and fill out the form
browser.visit(authorization_url)
username_browser = browser.find_by_id('username').fill(username)
password_browser = browser.find_by_id('password').fill(password)
#Click the login button
submit = browser.find_by_id('accept').first.click()
#User selects two factor authentication method and types in the code. Waits for user to say 'Done'
user_input = input('Select done when you have successfully completed two factor authentication: ')
if user_input == 'Done':
#Grab the url from the browser (mainly the info after the code)
new_url = browser.url
parse_url = urllib.parse.unquote(new_url.split('code=')[1])
#browser.quit()
#Define information needed to get an access and refresh token
api_url = r'https://api.tdameritrade.com/v1/oauth2/token'
headers = {'Content-Type':"application/x-www-form-urlencoded"}
data = {'grant_type':'authorization_code',
'access_type':'offline',
'code':parse_url,
'client_id':client_id,
'redirect_uri':redirect_uri}
#Post the data to get the token
auth_reply_json = requests.post(url=api_url,headers=headers,data=data)
auth_reply=auth_reply_json.json()
#Get the access and refresh token
access_token = auth_reply['access_token']
refresh_token = auth_reply['refresh_token']
print('Refresh token: ' + refresh_token)