views:

682

answers:

3

Okay well I try to login into facebook using python. but well it doesn't work and I've really no idea why

By google I found this result for logging into a website

unfortunately it does not work for me...

It seems to be that there is going something wrong with the cookies, because facebook says to me that cookies aren't turned on when I print the source of the first fetch of the webpage.

I'm using python 3.1

and this is the code I use...

from http import cookiejar
from urllib import request, parse

if __name__ == '__main__':
    urlLogin = 'http://www.facebook.com/login.php'

    id    = 'my.email'
    passw = 'my.password'

    fieldId   = 'email'
    fieldPass = 'pass'

    cj = cookiejar.CookieJar()
    data = parse.urlencode({fieldId:id, fieldPass:passw})

    opener = request.build_opener(request.HTTPCookieProcessor(cj))

    request.install_opener(opener)

    usock = opener.open(urlLogin, data)
    pageSource = usock.read()
    usock.close()
    print(pageSource)

    usock = opener.open('http://www.facebook.com/home.php')
    pageSource = usock.read()
    usock.close()
    print(pageSource)
+1  A: 

Facebook provides public API for some languages including Python. You should be using it. I would doubt however that PyFacebook or other wrappers work with py3k.

SilentGhost
A: 

I have used ClientForm and I almost got it working but unfortunately had to leave the script for some time. I was trying to post something to Facebook from the command line with a Python script.

Maybe you could try ClientForm http://wwwsearch.sourceforge.net/ClientForm/

This is part of the code I was using:

#first login prompt
   login = opener.open(base_url % target_url)
   forms = ClientForm.ParseResponse(login, backwards_compat=False)
   login.close()
   forms[0]['email'] = email
   forms[0]['pass'] = passw
   res = opener.open(forms[0].click())

Hope it helps

Facundo
did you actually managed to login successfully? because, as I understand, OP needs a token to login, otherwise FB won't let you in.
SilentGhost
I was trying to post an url. I get past the first login prompt and landed on the page where you have to choose a picture and a comment. I didn't make it work for that page, maybe because of some javascript magic.
Facundo
A: 

FB requests cookies when perform a login action, so you just need to add a line

usock = opener.open(urlLogin)

before

usock = opener.open(urlLogin, data)

to get a cookie before you actually login, then it will be okay.

I just tested a similar code, and it worked. Hope it helps.

lemuria