You need to send the full form. The easiest way to find out what Facebook expects is to use the Google Chrome Developer Tools to track your web requests.
To simplify your life, I tracked my own Facebook login and reproduced it below (with personal information edited, obviously), with non-essential information:
Request URL:https://m.facebook.com/login.php?refsrc=https%3A%2F%2Fm.facebook.com%2F&refid=8 Request Method:POST Form Data: lsd:AVqAE5Wf charset_test:€,´,€,´,水,,Є version:1 ajax:0 width:0 pxr:0 gps:0 m_ts:1392974963 li:cxwHUxatQiaLv1nZEYPp0aTB email:... pass:... login:Log In
As you can see, the form contains many fields. All must be provided so that you can enter. Email and password will be provided with your code. The rest of the fields actually have their own HTML values that Facebook serves you. This means that in order to emulate the browser login, you need to follow these steps:
- Make a GET on the login page (
https://m.facebook.com/ ) - Use an HTML parsing library (such as BeautifulSoup) to parse HTML and find the default values of form fields.
- The default values are in the
<input> HTML elements under the #login_form element. You will want to find them by name (e.g. charset_test ) and then pull out their value attribute. - Developing how to do this is beyond the scope of this answer, so I'm not going to go into it.
Combine the default values of the form fields with your email and password, for example:
data = { 'lsd': lsd, 'charset_test': csettest, 'version': version, 'ajax': ajax, 'width': width, 'pxr': pxr, 'gps': gps, 'm_ts': mts, 'li': li, } data['email'] = email data['pass'] = pass data['login'] = 'Log In'
Submit your username using the Session request:
s = requests.Session() r = s.post(url, data=data) r.raise_for_status()
Send all your future HTTP traffic through Session .
As you can see, this is a non-trivial way of doing things. This is because programs were not expected to use the website to log in: instead, you should use the SDK or their web API .
Lukasa Feb 21 '14 at 9:43 2014-02-21 09:43
source share