-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathaccess_token.py
52 lines (44 loc) · 1.88 KB
/
access_token.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
# -*- coding: utf-8 -*-
from kiteconnect import KiteConnect
from selenium import webdriver
import time
import os
cwd = os.getcwd()
def autologin():
token_path = "api_key.txt"
key_secret = open(token_path, 'r').read().split()
kite = KiteConnect(api_key=key_secret[0])
service = webdriver.chrome.service.Service('./chromedriver')
service.start()
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options = options.to_capabilities()
driver = webdriver.Remote(service.service_url, options)
driver.get(kite.login_url())
driver.implicitly_wait(10)
username = driver.find_element_by_xpath(
'/html/body/div[1]/div/div[2]/div[1]/div/div/div[2]/form/div[1]/input')
password = driver.find_element_by_xpath(
'/html/body/div[1]/div/div[2]/div[1]/div/div/div[2]/form/div[2]/input')
username.send_keys(key_secret[2])
password.send_keys(key_secret[3])
driver.find_element_by_xpath(
'/html/body/div[1]/div/div[2]/div[1]/div/div/div[2]/form/div[4]/button').click()
pin = driver.find_element_by_xpath(
'/html/body/div[1]/div/div[2]/div[1]/div/div/div[2]/form/div[2]/div/input')
pin.send_keys(key_secret[4])
driver.find_element_by_xpath(
'/html/body/div[1]/div/div[2]/div[1]/div/div/div[2]/form/div[3]/button').click()
time.sleep(10)
request_token = driver.current_url.split('=')[1].split('&action')[0]
with open('request_token.txt', 'w') as the_file:
the_file.write(request_token)
driver.quit()
autologin()
# generating and storing access token - valid till 6 am the next day
request_token = open("request_token.txt", 'r').read()
key_secret = open("api_key.txt", 'r').read().split()
kite = KiteConnect(api_key=key_secret[0])
data = kite.generate_session(request_token, api_secret=key_secret[1])
with open('access_token.txt', 'w') as file:
file.write(data["access_token"])