Unable to generate request and access token from kiteconnect

Hsadikot
Hi,


I am trying to generate the request and access token using the kiteconnect application. The issue being for using the api I am unable to generate the request and access token for the same. I was charged yesterday for the subscription and since then I am facing the issue.

The complete code is as given:

I am using selenium to automate the login process




# -*- coding: utf-8 -*-
"""
Created on Mon Nov 9 10:01:26 2020

@author: Administrator
"""

from kiteconnect import KiteConnect
from selenium import webdriver
import time
import os

cwd = os.chdir("C:/Users/hozef/OneDrive/Desktop/New_Logic")


def autologin():
token_path = "credentials.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(15)
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')

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')

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(15)
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("credentials.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"])




After I execute the code I get the following error:


TokenException: Token is invalid or has expired.



  • sujith
    It is mandatory by the exchange that a user has to log in manually at least once a day. We don't recommend automating login.
  • Hsadikot
    I logged in manually once and tried. Apart from this I have been using this app for about 6 months and never have I faced this issue
  • sujith
    @Hsadikot,
    We have been recommending not to automate login since the day we went live. It is against the regulation, so we recommend you don't automate login.
This discussion has been closed.