[Tutor] Facebook login using python

Aaron Misquith aaronmisquith at gmail.com
Mon Jan 28 16:40:33 CET 2013


I am working on a program which is used to login to facebook, get my friend
lists using graph api and export them into pdf format.
So far i'm able to login with facebook and get the friend list. I would
like if someone is able to provide me a code to export the output to pdf.
Another thing is that i'm hardcoding username, password and access tokens;
is there anyway this can be obtained using user input?

Code:

import urllib2, cookielib, re, os, sys
from facepy import GraphAPI
class Facebook():
def __init__(self, email, password):
self.email = email
self.password = password

cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
opener.addheaders = [('Referer', 'http://login.facebook.com/login.php'),
('Content-Type', 'application/x-www-form-urlencoded'),
('User-Agent', 'Mozilla/8.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.7)
Gecko/20091221 Firefox/3.5.7 (.NET CLR 3.5.30729)')]

self.opener = opener

def login(self):
url = 'https://login.facebook.com/login.php?login_attempt=1'
data =
"locale=en_US&non_com_login=&email="+self.email+"&pass="+self.password+"&lsd=20TOl"

usock = self.opener.open('http://www.facebook.com')
usock = self.opener.open(url, data)
if "Logout" in usock.read():
print "Logged in."
else:
print "failed login"
print usock.read()
sys.exit()
f = Facebook("Enter email", "Password")
f.login()
graph=GraphAPI('Enter Access code from fql in developer's page')

nik=graph.fql('select uid, name, friends from user where uid in (select
first_name, middle_name, last_name from friend where uid1 = me())')

print nik
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130128/26a084f3/attachment.html>


More information about the Tutor mailing list