7
votes

Since a few days I cannot log into facebook anymore with my script. The Facebook login page gives the error:

Cookies required, cookies are not enabled on your browser.

I dont know why this error appears because I accept cookies in my script. I hope someone could help me out, I have already googled and tryed different cookie methods.

import cookielib
import urllib2
import mechanize

br = mechanize.Browser()
cookiejar = cookielib.LWPCookieJar()
br.set_cookiejar( cookiejar )
br.set_handle_equiv( True )
br.set_handle_gzip( True )
br.set_handle_redirect( True ) 
br.set_handle_referer( True )
br.set_handle_robots( False )

br.set_handle_refresh( mechanize._http.HTTPRefreshProcessor(), max_time = 1)
br.addheaders = [ ( 'User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1' ) ]

user = "EMAIL"
pass = "PASSWORD"
url = "https://www.facebook.com/login.php"

#Open URL and submit
br.open(url)
br.select_form(nr=0)
br.form['email'] = user
br.form['pass'] = pass
response = br.submit()

#Opens website and write source to html-output.txt
fileobj = open("HTML-OUTPUT.txt","wb")
fileobj.write(response.read())
fileobj.close()
2
We do have the same problem, but only when facebook login loads through the CNA. - Dennis Tsoumas
Dennis, What do you mean with CNA? Maybe there is an solution, I've read something about export cookies from your real browser (i.e Firefox) and them import them into your script. How can I get in contact with you? I realy need to solve this problem. Are you from Netherlands? - Jesse kraal
We're getting the same problem here. It's frustrating, as we use the script to identify when there's a Facebook login issue so we can be aware before our users start complaining they can't log into our app ... - Clive Evans
You are not supposed to use tools to automatically login to Facebook, and they are actively taking measures against this – and most likely that’s what you are experiencing here. - CBroe
With an Mozilla Firefox plugin I have export a cookies.txt and add the following lines into the script. {'User-agent' : 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'} cj = cookielib.MozillaCookieJar('cookies.txt') cj.load() But I still have the same error: Cookies required, cookies are not enabled on your browser. Does someone knows the solution? - Jesse kraal

2 Answers

4
votes

Assuming that how do you log in is not important (as you were prepared to use your Mozilla cookies to do so), you can use the mobile website to accomplish it.

First, you log in to Facebook using its mobile version (which will not require cookies), then redirect your browser to the page you wanted to save.

Minor changes to your code:

user = "EMAIL"
passwd = "PASSWORD"
url = "https://m.facebook.com/login.php"

#Open URL and submit
br.open(url)
br.select_form(nr=0)
br.form['email'] = user
br.form['pass'] = passwd
br.submit()

response = br.open("https://www.facebook.com/")

#Opens website and write source to html-output.txt
fileobj = open("HTML-OUTPUT.txt","wb")
fileobj.write(response.read())
fileobj.close()
1
votes

I would recommend you to use this reusable application made for this purpose:

$ pip install django-oauth-tokens

And after in terminal

>>> from oauth_tokens.providers.facebook import FacebookAuthRequest
>>> req = FacebookAuthRequest(username='...', password='...')
>>> response = req.authorized_request(url='https://facebook.com')
>>> response.content.count(USER_FULL_NAME)
>>> fileobj = open("HTML-OUTPUT.txt","wb")
>>> fileobj.write(response.content)
>>> fileobj.close()
4