# How can generate \_forum\_session and \_t for an user through code/api call or without login to browser?

**URL:** https://meta.discourse.org/t/how-can-generate-forum-session-and-t-for-an-user-through-code-api-call-or-without-login-to-browser/233895
**Category:** Feature
**Tags:** sso
**Created:** [July 25, 2022, 3:12pm UTC](https://meta.discourse.org/t/how-can-generate-forum-session-and-t-for-an-user-through-code-api-call-or-without-login-to-browser/233895 "2022-07-25T15:12:35Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![Swarndeep\_Poddar](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/swarndeep_poddar/32/231276_2.png) [@Swarndeep\_Poddar](https://meta.discourse.org/u/Swarndeep_Poddar)
#### Post date: [July 25, 2022, 3:12pm UTC](https://meta.discourse.org/t/how-can-generate-forum-session-and-t-for-an-user-through-code-api-call-or-without-login-to-browser/233895/1 "2022-07-25T15:12:35Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [July 25, 2022, 4:41pm UTC](https://meta.discourse.org/t/how-can-generate-forum-session-and-t-for-an-user-through-code-api-call-or-without-login-to-browser/233895/2 "2022-07-25T16:41:48Z")

</div>

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)](https://meta.discourse.org/t/discourseconnect-official-single-sign-on-for-discourse-sso/13045)?

---

<div class="post-metadata">

### Author: ![Swarndeep\_Poddar](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/swarndeep_poddar/32/231276_2.png) [@Swarndeep\_Poddar](https://meta.discourse.org/u/Swarndeep_Poddar)
#### Post date: [July 25, 2022, 5:12pm UTC](https://meta.discourse.org/t/how-can-generate-forum-session-and-t-for-an-user-through-code-api-call-or-without-login-to-browser/233895/3 "2022-07-25T17:12:25Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [July 25, 2022, 5:16pm UTC](https://meta.discourse.org/t/how-can-generate-forum-session-and-t-for-an-user-through-code-api-call-or-without-login-to-browser/233895/4 "2022-07-25T17:16:26Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![Swarndeep\_Poddar](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/swarndeep_poddar/32/231276_2.png) [@Swarndeep\_Poddar](https://meta.discourse.org/u/Swarndeep_Poddar)
#### Post date: [July 25, 2022, 5:38pm UTC](https://meta.discourse.org/t/how-can-generate-forum-session-and-t-for-an-user-through-code-api-call-or-without-login-to-browser/233895/5 "2022-07-25T17:38:22Z")

</div>

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

---

<div class="post-metadata">

### Author: ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)
#### Post date: [July 25, 2022, 11:00pm UTC](https://meta.discourse.org/t/how-can-generate-forum-session-and-t-for-an-user-through-code-api-call-or-without-login-to-browser/233895/6 "2022-07-25T23:00:09Z")

</div>

> [@Swarndeep\_Poddar](#):
>
> must fill phone number and OTP manually

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

---

<div class="post-metadata">

### Author: ![Swarndeep\_Poddar](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/swarndeep_poddar/32/231276_2.png) [@Swarndeep\_Poddar](https://meta.discourse.org/u/Swarndeep_Poddar)
#### Post date: [July 26, 2022, 11:25am UTC](https://meta.discourse.org/t/how-can-generate-forum-session-and-t-for-an-user-through-code-api-call-or-without-login-to-browser/233895/7 "2022-07-26T11:25:53Z")

</div>

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

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [July 26, 2022, 2:14pm UTC](https://meta.discourse.org/t/how-can-generate-forum-session-and-t-for-an-user-through-code-api-call-or-without-login-to-browser/233895/8 "2022-07-26T14:14:57Z")

</div>

> [@Swarndeep\_Poddar](#):
>
> Why do you need the cookies  
> Ans - because without adding cookies.pkl file we are not able to login through login\_12.py file.

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](https://meta.discourse.org/t/13045?silent=true), that I linked to above. You can see that page for documentation and code examples.

---

<div class="post-metadata">

### Author: ![renato](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/renato/32/383632_2.png) [@renato](https://meta.discourse.org/u/renato)
#### Post date: [July 26, 2022, 3:31pm UTC](https://meta.discourse.org/t/how-can-generate-forum-session-and-t-for-an-user-through-code-api-call-or-without-login-to-browser/233895/9 "2022-07-26T15:31:25Z")

</div>

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](https://meta.discourse.org/t/using-discourse-as-an-identity-provider-sso-discourseconnect/32974).

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?

---

<div class="post-metadata">

### Author: ![Swarndeep\_Poddar](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/swarndeep_poddar/32/231276_2.png) [@Swarndeep\_Poddar](https://meta.discourse.org/u/Swarndeep_Poddar)
#### Post date: [July 26, 2022, 4:06pm UTC](https://meta.discourse.org/t/how-can-generate-forum-session-and-t-for-an-user-through-code-api-call-or-without-login-to-browser/233895/10 "2022-07-26T16:06:05Z")

</div>

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

```
`

---

<div class="post-metadata">

### Author: ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)
#### Post date: [July 26, 2022, 4:42pm UTC](https://meta.discourse.org/t/how-can-generate-forum-session-and-t-for-an-user-through-code-api-call-or-without-login-to-browser/233895/11 "2022-07-26T16:42:01Z")

</div>

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](https://discuss.dizzycoding.com/how-to-use-cookies-in-python-requests/) are some examples. I would recommend you to use a `Session` object.

If you want to parse the HTML output, you can use [`BeautifulSoup`](https://beautiful-soup-4.readthedocs.io/en/latest/).

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [July 26, 2022, 7:03pm UTC](https://meta.discourse.org/t/how-can-generate-forum-session-and-t-for-an-user-through-code-api-call-or-without-login-to-browser/233895/12 "2022-07-26T19:03:16Z")

</div>

> [@Swarndeep\_Poddar](#):
>
> we have file login\_12.py which test the discourse login functionality every 5 minute.

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?
