[Tutor] Facebook login using python
Dave Angel
davea at davea.name
Mon Jan 28 17:53:04 CET 2013
On 01/28/2013 10:40 AM, Aaron Misquith wrote:
> 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():
You should derive all classes from object, or from another class that
already does. Old-style classes are long deprecated (and eliminated in
Python 3.x)
> def __init__(self, email, password):
Please fix the indentation, and send as a text message, not html.
> 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
>
>
Before doing too much work on such an app, consider if you're violating
Facebook's terms of service. The following links may not be the right
ones, but it could get you started thinking, anyway.
http://developers.facebook.com/blog/post/2013/01/25/clarifying-our-platform-policies/
http://www.facebook.com/help/131112897028467/
As for creating a pdf from a simple text list, I think you'd be best
shelling out to a program that does just that. Naturally, the choices
vary by OS, and I have not recommendations.
As for username and password, why not use raw_input ?
--
DaveA
More information about the Tutor
mailing list