Access token automation using selenium python issue

Hi
I want to automate proccess of generating access token and i am able to automate using selenium without headless. but if i am tring to automate with headless i am getting timeout
error. Beacuse of this issue i am not able to deploye my script on server. i need to generate everyday access token and upload on server.
Please help me on this issue.

import requests as rq
from time import sleep
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import urllib.parse as urlparse
from pyotp import TOTP
import config




api_key = config.API_KEY
secret_key = config.SECRET_KEY
r_url = config.REDIRECT_URL
totp_key = config.TOTP_KEY
mobile_no = config.MOBILE_NO
pin = config.PIN
auth_url = f'https://api-v2.upstox.com/login/authorization/dialog?response_type=code&client_id={api_key}&redirect_uri={r_url}'


def generateAccessToken2():
    options = webdriver.ChromeOptions()
    options.add_argument(' --disable-dev-shm-usage')
    options.add_argument(' --headless')
    options.add_argument(' --disable-gpu')
    options.add_argument(' --no-sandbox')
    driver = webdriver.Chrome(options=options)
    driver.get(auth_url)
    wait = WebDriverWait(driver,30)
    wait.until(EC.presence_of_element_located((By.XPATH, '//input[@type="text"]'))).send_keys(mobile_no)
    wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="getOtp"]'))).click()
    totp = TOTP(totp_key).now()
    sleep(2)
    wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="otpNum"]'))).send_keys(totp)
    wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="continueBtn"]'))).click()
    sleep(2)
    wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="pinCode"]'))).send_keys(pin)
    wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="pinContinueBtn"]'))).click()
    sleep(2)
    token_url = driver.current_url
    parsed = urlparse.urlparse(token_url)
    driver.close()
    code = urlparse.parse_qs(parsed.query)['code'][0]
    url = 'https://api-v2.upstox.com/login/authorization/token'
    headers = {
        'accept': 'application/json',
        'Api-Version': '2.0',
        'Content-Type': 'application/x-www-form-urlencoded'}

    data = {
        'code': code,
        'client_id': api_key,
        'client_secret': secret_key,
        'redirect_uri': r_url,
        'grant_type': 'authorization_code'}

    response = rq.post(url, headers=headers, data=data)
    jsr = response.json()

    with open('access_token.txt','w') as file:
        file.write(jsr['access_token'])
        print("done")


generateAccessToken2()

error message

“Access to fetch at ‘https://service.upstox.com/login/open/v6/auth/1fa/otp/generate’ from origin ‘https://login.upstox.com’ has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource. If an opaque response serves your
needs, set the request’s mode to ‘no-cors’ to fetch the resource with CORS disabled.”, source: https://login.upstox.com/ (0)
[0317/120045.323:INFO:CONSOLE(0)] “Third-party cookie will be blocked. Learn more in the Issues tab.”, source: https://login.upstox.com/ (0)
[0317/120045.324:INFO:CONSOLE(0)] “Third-party cookie will be blocked. Learn more in the Issues tab.”, source: https://login.upstox.com/ (0)
[0317/120045.324:INFO:CONSOLE(0)] “Third-party cookie will be blocked. Learn more in the Issues tab.”, source: https://login.upstox.com/ (0)
[0317/120045.325:INFO:CONSOLE(0)] “Third-party cookie will be blocked. Learn more in the Issues tab.”, source: https://login.upstox.com/ (0)
[0317/120045.326:INFO:CONSOLE(0)] “Third-party cookie will be blocked. Learn more in the Issues tab.”, source: https://login.upstox.com/ (0)
[0317/120045.488:INFO:CONSOLE(0)] “Third-party cookie will be blocked. Learn more in the Issues tab.”, source: https://login.upstox.com/ (0)
Traceback (most recent call last):
File “.\test.py”, line 67, in
generateAccessToken2()
File “.\test.py”, line 36, in generateAccessToken2
wait.until(EC.presence_of_element_located((By.XPATH, ‘//*[@id=“otpNum”]’))).send_keys(totp)
File “C:\Users\RahulMars\Desktop\tradingBot\env\lib\site-packages\selenium\webdriver\support\wait.py”, line 105, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
Stacktrace:
GetHandleVerifier [0x00007FF63805AD02+56930]

Thanks
Rahul

Upstox API does not support or recommend automating access token generation in light of SEBI and regulatory guidelines. Users are required to log in manually once daily by adhering to the specified login procedures.

Thank you.

Thanks for the response but how can we automate, do you have any logic or any idea as other brokers are fully automating and rules are same for all then why are you not allowing right now, I am generating tokens every day and honestly I really hate this process.

Thank you

1 Like