-
Notifications
You must be signed in to change notification settings - Fork 0
/
access-session_token_generation
84 lines (62 loc) · 3.21 KB
/
access-session_token_generation
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from kiteconnect import KiteConnect
from kiteconnect import KiteTicker
import undetected_chromedriver as uc
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium import webdriver
import time
import pyotp
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.microsoft import EdgeChromiumDriverManager
import certifi
def login_in_zerodha(api_key, api_secret, user_id, user_pwd, totp_key):
print('inside')
#driver = uc.Chrome()
#driver = webdriver.Chrome(executable_path=r'C:\Program Files\Google\Chrome\Application\chrome.exe')
driver = webdriver.Chrome(ChromeDriverManager().install())
#driver = webdriver.Edge(EdgeChromiumDriverManager().install())
#driver.get(f'https://kite.trade/connect/login?api_key={api_key}&v=3')
driver.get(f'https://kite.zerodha.com/connect/login?v=3&api_key={api_key}')
time.sleep(5)
login_id = WebDriverWait(driver, 10).until(lambda x: x.find_element("xpath", '//*[@id="userid"]'))
login_id.send_keys(user_id)
pwd = WebDriverWait(driver, 10).until(lambda x: x.find_element("xpath", '//*[@id="password"]'))
pwd.send_keys(user_pwd)
'''check1 = WebDriverWait(driver, 10).until(lambda x: x.find_element("xpath", '//*[@class="su-checkbox-box"]'))
check1.click()'''
submit = WebDriverWait(driver, 10).until(lambda x: x.find_element("xpath", '//*[@id="container"]/div/div/div[2]/form/div[4]/button'))
submit.click()
totp = WebDriverWait(driver, 10).until(lambda x: x.find_element("xpath", '//*[@label="External TOTP"]'))
authkey = pyotp.TOTP(totp_key)
totp.send_keys(authkey.now())
continue_btn = WebDriverWait(driver, 10).until(lambda x: x.find_element("xpath", '//*[@id="container"]/div[2]/div/div[2]/form/div[2]/button'))
continue_btn.click()
'''pwd1 = WebDriverWait(driver, 10).until(lambda x: x.find_element("xpath", '//*[@id="password"]'))
pwd1.send_keys(user_pwd)
submit1 = WebDriverWait(driver, 10).until(lambda x: x.find_element("xpath", '//*[@id="container"]/div[2]/div/div/form/div[3]/button'))
submit1.click()
totp1 = WebDriverWait(driver, 10).until(lambda x: x.find_element("xpath", '//*[@label="External TOTP"]'))
authkey1 = pyotp.TOTP(totp_key)
totp1.send_keys(authkey.now())
continue_btn1 = WebDriverWait(driver, 10).until(lambda x: x.find_element("xpath", '//*[@id="container"]/div[2]/div/div/form/div[2]/button'))
continue_btn1.click()'''
time.sleep(2)
url = driver.current_url
print('urllll',url)
initial_token = url.split('request_token=')[1]
request_token = initial_token
#request_token = initial_token.split('=')[0]
request_token = request_token.split('&')[0]
print('\n\n\n\Request id',request_token)
driver.close()
kite = KiteConnect(api_key = api_key)
print(request_token)
data = kite.generate_session(request_token, api_secret=api_secret)
#print(data)
print('access token',data['access_token'])
kite.set_access_token(data['access_token'])
return kite
if __name__ == '__main__':
print('inside main')
kiteobj = login_in_zerodha(api_key, api_secret, user_id, user_pwd, totp_key)
print('\n\n\n\nSession id',kiteobj.access_token)