Requests needs cookies for login - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Requests needs cookies for login (/thread-4147.html) |
Requests needs cookies for login - justasug - Jul-26-2017 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) RE: Requests needs cookies for login - nilamo - Jul-26-2017 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. RE: Requests needs cookies for login - justasug - Jul-27-2017 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]" 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 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) |