Python Forum
Requests needs cookies for login
Thread Rating:
  • 3 Vote(s) - 3.67 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Requests needs cookies for login
#1
I am trying to login into my email account through a website and run an action when I get a new email (I can't use IMAP, because it's a paid feature). I use the 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 mail.com mailbox you must enable cookies in your browser."

Can someone tell me what I am doing wrong? I checked the cookies for the Session object and it has cookies.

Here's what I have. Feel free to use the testing account information to login and inspect the POST request in your web browser and test it with Python yourself. Note: check the comments, I had to remove the https:// parts of the address out, because it doesn't let me to post "clickable links" yet, not even in code tags.
 
import requests

user = "[email protected]"
psswd = "fortesting"
# had to leave out the https part because it doesn't allow me to post "clickable links"
website = "www.mail.com"
# same for this as above
POST_url = "login.mail.com/login#.1258-header-login1-2"

# from the POST request info from Firefox's developer tools
params = {
    "service": "mailint",
    "uasServiceID": "mc_starter_mailcom",
    # same as above, had to leave https:// out for the successURL, loginFailedURL and loginErrorURL
    "successURL": "$(clientName)-$(dataCenter).mail.com/login",
    "loginFailedURL": "www.mail.com/int/logout/?ls=wd",
    "loginErrorURL": "www.mail.com/int/logout/?ls=te",
    "edition": "int",
    "lang": "en",
    "usertype": "standard",
    "username": user,
    "password": psswd
}

sess = requests.Session()
response = sess.post(POST_url, data=params)
print(response.text)
Reply
#2
You might need to make a GET (or HEAD, if requests/the server handle that) request to get a session id, and then pass that to the url you're POSTing to. As it is, you're not sending any cookies, so you need to do something to get a cookie that the server will accept.
Reply
#3
I used GET before with the Session() object to get the cookies.

These are the cookies which are sent when I log in from my browser (according to the Firefox developer tools):
Quote:cookieKID    "kid@[email protected]"
cookiePartner    "kid@[email protected]"
ushallpass    "true"

Line 41 below shows that those cookies are set. I just added the "ushallpass" cookie myself in line 40.

Looking at the request headers sent from my browser, I have this:
Quote:Host: login.mail.com
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:54.0) Gecko/20100101 Firefox/54.0
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
Cookie: cookieKID=kid%40autoref%40mail.com; cookiePartner=kid%40autoref%40mail.com; ushallpass=true
Connection: keep-alive
Upgrade-Insecure-Requests: 1

I added those too, but I had to remove "Host", "Content-Length" because I didn't get a response otherwise. I also remove "Cookie" because I pass those either way.

I don't know what else to try. Does anyone have ideas?

Here's my current attempt with all the changes:
import requests

user = "[email protected]"
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": "[email protected]",
    "password": "fortesting"
}

browser_headers = {
#    "Host": "login.mail.com",
#    "User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:54.0) Gecko/20100101 Firefox/54.0",
    "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",
#    "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, headers=browser_headers)
sess.cookies["ushallpass"] = "true"
print(sess.cookies)
a = sess.post(POST_url, cookies=sess.cookies, headers=browser_headers, data=params)
print(a.text)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to accept facebook cookies using python selenium? pablo86ad 0 172 Apr-06-2024, 09:19 PM
Last Post: pablo86ad
  in a login interface when i try login with a user supposed to say test123 but nothing NullAdmin 3 2,264 Feb-20-2021, 04:43 AM
Last Post: bowlofred
  Retrieving Cookies whois1230 2 2,172 Nov-21-2020, 12:01 PM
Last Post: snippsat
  Cookies Donald 3 2,821 Apr-15-2020, 07:06 AM
Last Post: DPaul

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020