2

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)
Likdoyaya
  • 21
  • 5
  • It's not up to you to send a _**cookie**_ at your first request. Read this Example: [Login to Facebook using python requests](https://stackoverflow.com/a/43983934/7414759). – stovfl Jul 28 '17 at 12:24
  • I don't think I'm sending cookies during the first request. I made the `sess.get(website)` request and after that just added a cookie manually after that to the already existing ones (the "ushallpass" cookie). I checked the example you linked and I'm doing pretty much the same thing, but it's not working for this website. – Likdoyaya Jul 29 '17 at 21:08
  • I assume _this website_ complain about the changed _cookie_. – stovfl Jul 29 '17 at 22:11
  • I just tried it without adding a cookie manually and I get the same result. – Likdoyaya Jul 30 '17 at 06:32
  • To clarify, have you tried **resending** the **same** cookie you got from the **first** request? – stovfl Jul 30 '17 at 08:38
  • Yes, I have. I edited the OP to show how. Unfortunately I'm still getting the same result. – Likdoyaya Aug 01 '17 at 08:04
  • `sess.get(website)` as the First request is not enought. You have to Login at your First request e.g. `response = session.post(, data={'username': email, 'pass': password}`) – stovfl Aug 01 '17 at 09:17
  • Ah, sorry for getting that wrong. I just tried it exactly how you said, but I'm still getting the "Your browser must have cookies enabled" response HTML. – Likdoyaya Aug 02 '17 at 13:29
  • Sorry, I'm off, no more I can think of. – stovfl Aug 02 '17 at 13:49

0 Answers0