I am trying to login into my email account through a website (cannot use IMAP). I use the Python Requests module.
I looked at the POST request Firefox made when logging in and copied the data. When I submit it and look at the response's content, it says "To access your mailbox you must enable cookies in your browser."
I checked the cookies for the Session object and it has cookies. What am I doing wrong?
Here's what I have, with sample account data that can be used for testing logging in.
import requests
user = "for_testing@mail.com"
psswd = "fortesting"
website = "https://www.mail.com"
POST_url = "https://login.mail.com/login#.1258-header-login1-2"
user_agent = "Mozilla/5.0 (Linux; Android 6.0.1; SM-G920V Build/MMB29K)"
# from the POST request info from Firefox's developer tools
params = {
"service": "mailint",
"uasServiceID": "mc_starter_mailcom",
"successURL": "https://$(clientName)-$(dataCenter).mail.com/login",
"loginFailedURL": "https://www.mail.com/int/logout/?ls=wd",
"loginErrorURL": "https://www.mail.com/int/logout/?ls=te",
"edition": "int",
"lang": "en",
"usertype": "standard",
"username": "for_testing@mail.com",
"password": "fortesting"
}
# these are the headers sent via Firefox upon a successful login
browser_headers = {
# leaving this uncommented gives us an error page "request not understood"
# "Host": "login.mail.com",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language": "en-US,en;q=0.5",
"Accept-Encoding": "gzip, deflate, br",
"Referer": "https://www.mail.com/int/",
"Content-Type": "application/x-www-form-urlencoded",
"Content-Length": "358",
# not sending this, because the cookies are already set
# "Cookie": "cookieKID=kid%40autoref%40mail.com; cookiePartner=kid%40autoref%40mail.com; ushallpass=true",
"Connection": "keep-alive",
"Upgrade-Insecure-Requests": "1",
"User-Agent": user_agent
}
sess = requests.Session()
sess.get(website)
# setting this cookie manually
sess.cookies["ushallpass"] = "true"
print(sess.cookies)
a = sess.post(POST_url, headers=browser_headers, data=params)
# this shows that we got the error website and not the actual account page
print(a.text)
EDIT: The issue persists even after trying not to manually add the "ushallpass" cookie. Like this:
sess = requests.Session()
sess.get(website)
a = sess.post(POST_url, headers=browser_headers, data=params)
# this shows that we got the error website and not the actual account page
print(a.text)
EDIT 2: Trying to pass the unmodified cookies gotten with the first request explicitly (I think the Session() object does that automatically).
sess = requests.Session()
sess.get(website)
a = sess.post(POST_url, cookies=sess.cookies, data=params)
print(a.text)