How can generate _forum_session and _t for an user through code/api call or without login to browser?

I have written a code for generation cookies ( _forum_session and _t) through selenium and pyhton. But in this case also we must fill manually phone number, OPT to validate. I want to make generate cookies fully automatically through code , don’t want to fill manually anything. How can achieve this?

Can you back up and describe what problem having those cookies is going to solve?

What are you trying to accomplish? Are you trying to have Discourse authenticate against your app? Maybe look at DiscourseConnect - Official Single-Sign-On for Discourse (sso)?

Through generate_cookies.py, I received the cookies.pkl which I use to test automatically login in every 5 minute.

So i have a login_12.py file which call in every 5 minute to test my logins
login_12.py (below is some code example)

import cookies.pkl
driver.get(“https://forum.discourse.org/”)
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, ‘/html/body’)))
logger.debug(jlog.format(timestamp=datetime.now(), level=“DEBUG”, status=“Executing”, message=“Homepage reloaded”))
time.sleep(3)

cookies = pickle.load(open("cookies.pkl", "rb"))
for cookie in cookies:
    print(cookie)
    driver.add_cookie(cookie)
logger.debug(jlog.format(timestamp=datetime.now(), level="DEBUG", status="Executing", message="Added cookies"))
time.sleep(2)

driver.get(driver.current_url)
    logger.debug(jlog.format(timestamp=datetime.now(), level="DEBUG", status="Executing", message="Homepage reloaded"))
Want to get generate cookies automatically without using browser. so whole process will be automatically.

You’re still describing your proposed solution rather than the problem that you are trying to solve.

Why do you need the cookies if you don’t need a browser?

Are you trying to have your external app be able to <do something> to Discourse? Maybe just use an API key?

1 Like

Why do you need the cookies
Ans - because without adding cookies.pkl file we are not able to login through login_12.py file.

As of now for generating cookies we must fill phone number and OTP manually in chrome driver browser. I so i dont want to fill anything manually that why i don’t want to use browser for generating cookies.

generate_cookies.py (example code)

from selenium import webdriver
import undetected_chromedriver as uc

options = uc.ChromeOptions()
#options.add_argument('--headless')
options.add_argument("--disable-extensions")
options.add_argument("--disable-popup-blocking")

# login
GMAIL = 'discourse.mytest@ymail.com'
PASSWORD = 'test123'

driver.get("https://forun.discourse.org/")
print(driver.title)
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, '/html/body')))
print('Homepage loaded')


WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.CLASS_NAME, 'login-button')))
login = driver.find_elements_by_class_name("login-button")[0]
print(login)
login.send_keys(Keys.RETURN)
print(driver.current_url)
driver.get(driver.current_url)
print("email")
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, '/html/body')))
print('Email page loaded')
#print(driver.page_source)

driver.find_element_by_xpath("/html/body/div[1]/div[1]/div[2]/div/div[2]/div/div/div[2]/div/div[1]/div/form/span/section/div/div/div[1]/div/div[1]/div/div[1]/input").send_keys(GMAIL)
driver.find_element_by_xpath("/html/body/div[1]/div[1]/div[2]/div/div[2]/div/div/div[2]/div/div[1]/div/form/span/section/div/div/div[1]/div/div[1]/div/div[1]/input").send_keys(Keys.RETURN)
print("Email entered")

time.sleep(3)

WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, '/html/body')))
print('Password page loaded')
#print(driver.page_source)

driver.find_element_by_xpath("/html/body/div[1]/div[1]/div[2]/div/div[2]/div/div/div[2]/div/div[1]/div/form/span/section/div/div/div[1]/div[1]/div/div/div/div/div[1]/div/div[1]/input").send_keys(PASSWORD)
driver.find_element_by_xpath("/html/body/div[1]/div[1]/div[2]/div/div[2]/div/div/div[2]/div/div[1]/div/form/span/section/div/div/div[1]/div[1]/div/div/div/div/div[1]/div/div[1]/input").send_keys(Keys.RETURN)
print("Password entered")
time.sleep(3)

WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, '/html/body')))
print('Login completed')
print(driver.title)

#print(driver.find_element_by_xpath("/html/body").get_attribute('innerHTML'))

if not "Discourse Forum" in driver.title:
    raise Exception('login Error')
else:
    print("Homepage loaded")
driver.get(driver.current_url+'/c/announcements/5')
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, '/html/head/title')))
print('Trying announcements page')
print(driver.title)

Finally want to achieve this.

if(get expiry from headless browser’s local-storage >= now )

{

#run generate cookies (Issue with this part to make automatically , currently some parts is mannually)

#upload cookies.pkl file to appropriate directory

}else{

#run lopin_12.py to login

}

#access the local-storage for headless chrome browser

#add/update the expiry date in local-storage variable

How and why?
You can fill in the phone number (where is that being asked?) automatically and turn off OTP for that user?

1 Like

@RGJ Is there any endpoints or api available which we can call to get a valid login access token?

If what you are trying to do is to let people log in to Discourse using your other app, or to log in to your other app using Discourse, then you want to implement DiscourseConnect, that I linked to above. You can see that page for documentation and code examples.

Yeah, it’s still not clear to me what exactly you’re trying to achieve.

If you need to integrate another webpage using Discourse’s auth as the source of truth, you can use Discourse as a SSO provider.

If you need to make server-to-server calls to Discourse’s API, you can create API keys on /admin/api/keys.

Can you share what is the functional need you’re trying to address?

2 Likes

we have file login_12.py which test the discourse login functionality every 5 minute.

we had to add cookies(cookies.pkl) in login_12.py to login properly.

How we got cookies (cookies.pkl)?
we have another generate_cookies.py , in this file we have hard coded user email and password, using this we generate cookies for that particular user and write a cookies.pkl file.

For generation cookies we use browser which we want to avoid and this step want to do automatically through code withhout using browser.

content of generate_cookies.py

from selenium import webdriver import undetected_chromedriver as uc from selenium.webdriver.common.keys import Keys import time from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import pickle

options = uc.ChromeOptions()
#options.add_argument(‘–headless’)
options.add_argument(“–disable-extensions”)
options.add_argument(“–disable-popup-blocking”)
options.add_argument(“–profile-directory=Default”)
options.add_argument(“–ignore-certificate-errors”)
options.add_argument(“–disable-plugins-discovery”)
options.add_argument(“–incognito”)
options.add_argument(“user_agent=DN”)

if name == “main”: #Needed for Windows only (Bug in undetected_chromedriver)
driver = uc.Chrome(options=options)

# login
GMAIL = 'discourse.testing@gmail.com'
PASSWORD = 'discoursetest123'

driver.get("https://forum.discourse.org/")
print(driver.title)
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, '/html/body')))
print('Homepage loaded')


WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.CLASS_NAME, 'login-button')))
login = driver.find_elements_by_class_name("login-button")[0]
print(login)
login.send_keys(Keys.RETURN)
print(driver.current_url)
driver.get(driver.current_url)
print("email")
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, '/html/body')))
print('Email page loaded')
#print(driver.page_source)

driver.find_element_by_xpath("/html/body/div[1]/div[1]/div[2]/div/div[2]/div/div/div[2]/div/div[1]/div/form/span/section/div/div/div[1]/div/div[1]/div/div[1]/input").send_keys(GMAIL)
driver.find_element_by_xpath("/html/body/div[1]/div[1]/div[2]/div/div[2]/div/div/div[2]/div/div[1]/div/form/span/section/div/div/div[1]/div/div[1]/div/div[1]/input").send_keys(Keys.RETURN)
print("Email entered")

time.sleep(3)

WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, '/html/body')))
print('Password page loaded')
#print(driver.page_source)

driver.find_element_by_xpath("/html/body/div[1]/div[1]/div[2]/div/div[2]/div/div/div[2]/div/div[1]/div/form/span/section/div/div/div[1]/div[1]/div/div/div/div/div[1]/div/div[1]/input").send_keys(PASSWORD)
driver.find_element_by_xpath("/html/body/div[1]/div[1]/div[2]/div/div[2]/div/div/div[2]/div/div[1]/div/form/span/section/div/div/div[1]/div[1]/div/div/div/div/div[1]/div/div[1]/input").send_keys(Keys.RETURN)
print("Password entered")
time.sleep(3)

WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, '/html/body')))
print('Login completed')
print(driver.title)

#print(driver.find_element_by_xpath("/html/body").get_attribute('innerHTML'))

if not "Discourse Forum" in driver.title:
    raise Exception('login Error')
else:
    print("Homepage loaded")
driver.get(driver.current_url+'/c/announcements/5')
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, '/html/head/title')))
print('Trying announcements page')
print(driver.title)
if not "Latest Announcements topics - Discourse Forum" in driver.title:
    raise Exception('login Error function')
print("Success!")

pickle.dump( driver.get_cookies() , open("cookies2.pkl","wb"))

This is more a Python question.

You can use the Requests library in Python to make your requests. It supports cookies in various ways, here are some examples. I would recommend you to use a Session object.

If you want to parse the HTML output, you can use BeautifulSoup.

1 Like

So what you’re trying to accomplish is to continually test that logins work? You aren’t trying to do anything. You just want to make sure that if someone tries to log into your site, it will (probably) work?