pauldespre 0 Newbie Poster

I want to write a script that logs an user into Facebook and initialize oauth dialog to get the access_token(I don't want to log in into a website using facebook account!). I don't want to use facebook-sdk,pyfb etc.

This is what I've tried:

import sys, urllib2,json,urlparse,webbrowser, os, cookielib, re
class Facebook():
        def __init__(self, email, password):
       = email
                self.password = password

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

                self.opener = opener

        def login(self):
                url = ''
                data ="locale=en_US&non_com_login=&email=""&pass="+self.password+"&lsd=20TOl"

                usock ='')
                usock =, data)
                if "Logout" in
                        print "Logged in."
                        print "failed login"

f = Facebook("email", "pass")
#oauth dialog

url = ""+APP_ID+"&redirect_uri="+REDIRECT_URL+"&scope="+SCOPE+"&response_type=code"

#get code parameter from oauth dialog
#it will work only if the user had already granted the required permissions
req = urllib2.Request(url)
response = urllib2.urlopen(req)

print response.geturl()

This script prints out:
Logged in

and the last line prints:

but what I expect is something like this: some alphanumerical code.

If I access the URL stored in "url" variable from the browser, it will redirect me to what I'm expecting. The same thing happens if I access from browser the output from last line of code. So, why I don't have in the "response" variable the final redirect URL (that with from "url" page?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.