I try to automate the login to this site via requests, by working with session for the first time.
https://www.tennisenpadelvlaanderen.be/login
I would like to for example get the contents of this page, when a user is logged in.
https://www.tennisenpadelvlaanderen.be/dashboard/mijn-gegevens?
I created a login on the site, for this purpose only:
Login: hoegje+pepepepe@gmail.com Password: PepePepe
But when I try to do the POST to the login page, I get a 200 OK response, while on the site with devtools I get a 302 redirect and it redirects me to the first page of the site.
s = requests.session()
#Try to accept cookies
cookieData = {'cookies_functional': 'true', 'cookies_analytics': 'true', 'cookies_social': 'false',
'cookies_media': 'false', 'cookies_accepted': 'true'}
expires = datetime.datetime.utcnow() + datetime.timedelta(weeks=52) # expires in 1 year
for aCookieKey, aCookieValue in cookieData.items():
acceptCookiesCookie = requests.cookies.create_cookie(aCookieKey, aCookieValue, domain='www.tennisenpadelvlaanderen.be', path='/', expires=expires.timestamp())
s.cookies.set_cookie(acceptCookiesCookie)
acceptCookiesCookie = requests.cookies.create_cookie(aCookieKey, aCookieValue, path='www.tennisenpadelvlaanderen.be/', expires=expires.timestamp())
s.cookies.set_cookie(acceptCookiesCookie)
# Response sets cookie: INGRESSCOOKIE = 1695315839.247.1714.732149|60903facce0df50e07e804a3a5c147b8
firstResponse = s.get("https://www.tennisenpadelvlaanderen.be/login", headers={
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
"accept-encoding": "gzip, deflate, br", "accept-language": "en-US,en;q=0.9,nl;q=0.8,fr;q=0.7,de;q=0.6",
"referer": "https://www.tennisenpadelvlaanderen.be/", "upgrade-insecure-requests": "1",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36 Edg/117.0.2045.31"})
print('=' * 50)
print(s.cookies)
print(firstResponse.cookies)
print('=' * 50)
print(s.cookies)
submitCookies = s.cookies
#{originalJsessionId}
lfrSessionStateTs = str(int(datetime.datetime.now().timestamp() * 1000))
originalJsessionId = s.cookies.get('JSESSIONID')
originalIngressCookie = s.cookies.get('INGRESSCOOKIE')
print(f"originalJsessionId {originalJsessionId}")
print(f'f1 {firstResponse}')
print(f'f2 {firstResponse.cookies}')
print(f'f3 {firstResponse.headers}')
print(f'f4 {firstResponse.request}')
print(f'f5 {firstResponse.text.find("Liferay.authToken")}')
i1 = firstResponse.text.find("Liferay.authToken")
i2 = firstResponse.text.find(";", i1)
Liferay_authToken = firstResponse.text[i1:i2]
print(Liferay_authToken)
i1 = Liferay_authToken.find("'") + 1
i2 = Liferay_authToken.rfind("'")
pAuthRandom = Liferay_authToken[i1:i2]
secondResponse = s.get("https://www.tennisenpadelvlaanderen.be/login", headers={
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
"accept-encoding": "gzip, deflate, br", "accept-language": "en-US,en;q=0.9,nl;q=0.8,fr;q=0.7,de;q=0.6",
"cache-control": "max-age=0",
"cookie": f"INGRESSCOOKIE={originalIngressCookie}; JSESSIONID={originalJsessionId}; COOKIE_SUPPORT=true; GUEST_LANGUAGE_ID=nl_NL; LFR_SESSION_STATE_20101={lfrSessionStateTs}; cookies_functional=true; cookies_analytics=true; cookies_social=true; cookies_media=true; cookies_accepted=true",
"referer": "https://www.tennisenpadelvlaanderen.be/login", "upgrade-insecure-requests": "1",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36 Edg/117.0.2045.31"},
cookies=submitCookies)
print(f's1 {secondResponse}')
print(f's2 {secondResponse.cookies}')
print(f's3 {secondResponse.headers}')
print(f's4 {secondResponse.request}')
print(f's5 {secondResponse.request}')
# Redirects to: https://www.tennisenpadelvlaanderen.be/dashboard/nieuws-en-evenementen
# TODO add content-length to headers: 'content-length': str(contentLength),
# import string
# characters = string.ascii_letters + string.digits
# pAuthRandom = ''.join(random.choice(characters) for _ in range(8))
print(f"pAuthRandom {pAuthRandom}")
dataToSubmit = {
"_com_liferay_login_web_portlet_LoginPortlet_formDate": f"{lfrSessionStateTs}",
"_com_liferay_login_web_portlet_LoginPortlet_saveLastPath": "false",
"_com_liferay_login_web_portlet_LoginPortlet_redirect": "",
"_com_liferay_login_web_portlet_LoginPortlet_doActionAfterLogin": "false",
"_com_liferay_login_web_portlet_LoginPortlet_portletPath": "%2Flogin",
"_com_liferay_login_web_portlet_LoginPortlet_login": "hoegje%2Bpepepepe%40gmail.com",
"_com_liferay_login_web_portlet_LoginPortlet_password": "PepePepe",
"p_auth": pAuthRandom
}
dataStringToSubmit = ''
for aKey, aValue in dataToSubmit.items():
dataStringToSubmit += f'{aKey}={aValue}&'
dataStringToSubmit = dataStringToSubmit[:-1]
contentLength = len(str(dataStringToSubmit)) # 2 brackets, 4 for None
postReply = s.post("https://www.tennisenpadelvlaanderen.be/login",
params={"p_p_id": "com_liferay_login_web_portlet_LoginPortlet", "p_p_lifecycle": "0", "p_p_state": "normal",
"p_p_mode": "view",
"_com_liferay_login_web_portlet_LoginPortlet_javax.portlet.action": "%2Flogin%2Flogin",
"_com_liferay_login_web_portlet_LoginPortlet_mvcRenderCommandName": "%2Flogin%2Flogin"},
data=dataStringToSubmit,
headers={
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
"accept-encoding": "gzip, deflate, br", "accept-language": "en-US,en;q=0.9,nl;q=0.8,fr;q=0.7,de;q=0.6",
"cache-control": "max-age=0", 'Content-Type': 'application/x-www-form-urlencoded',
"cookie": f"INGRESSCOOKIE={originalIngressCookie}; JSESSIONID={originalJsessionId}; COOKIE_SUPPORT=true; GUEST_LANGUAGE_ID=nl_NL; cookies_functional=true; cookies_analytics=true; cookies_social=true; cookies_media=true; cookies_accepted=true; LFR_SESSION_STATE_20101={lfrSessionStateTs}",
"origin": "https://www.tennisenpadelvlaanderen.be", 'content-length': str(contentLength),
"referer": "https://www.tennisenpadelvlaanderen.be/login", "upgrade-insecure-requests": "1",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36 Edg/117.0.2045.31",
'sec-ch-ua-mobile': '?0','sec-ch-ua-platform': "Windows", 'Sec-Fetch-Dest': 'document', 'Sec-Fetch-Mode': 'navigate',
'Sec-Fetch-Site': 'same-origin', 'Sec-Fetch-User': '?1', 'sec-gpc': '1', 'Upgrade-Insecure-Requests':'1',
'Connection': 'keep-alive'
},
cookies=s.cookies)
print(f'p1 {postReply}')
print(f'p2 {postReply.cookies}')
print(f'p3 {postReply.headers}')
print(f'p4 {postReply.request}')
print(f'p5 {postReply.request}')
# Response sets cookie: JSESSIONID = DECCD37CCA427785F666166AB6CB085E
finalResponse = s.get("https://www.tennisenpadelvlaanderen.be/dashboard/nieuws-en-evenementen", headers={
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
"accept-encoding": "gzip, deflate, br", "accept-language": "en-US,en;q=0.9,nl;q=0.8,fr;q=0.7,de;q=0.6",
"cache-control": "max-age=0",
"cookie": f"INGRESSCOOKIE={originalIngressCookie}; JSESSIONID={originalJsessionId}; COOKIE_SUPPORT=true; GUEST_LANGUAGE_ID=nl_NL; cookies_functional=true; cookies_analytics=true; cookies_social=true; cookies_media=true; cookies_accepted=true; LFR_SESSION_STATE_20101={lfrSessionStateTs}",
"referer": "https://www.tennisenpadelvlaanderen.be/login", "upgrade-insecure-requests": "1",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36 Edg/117.0.2045.31"},
cookies=s.cookies)
print(finalResponse)
print(finalResponse.headers)
print(finalResponse.text.find('Pepe'))
print(postReply.text)
Edit: added unredacted code, since the shorter version was not enough to reproduce the issue.
from Login to this site with requests
No comments:
Post a Comment