2

I am attempting to add functionality to my Django app: when a new post is approved, I want to update the corresponding Facebook Page's status with a message and a link to the post automatically. Basic status update.

I have downloaded and installed pyfacebook - and I have read through the tutorial from Facebook. I have also seen this suggestion here on SO:

import facebook
fb = facebook.Facebook('YOUR_API_KEY', 'YOUR_SECRET_KEY')
fb.auth.createToken()
fb.login() # THIS IS AS FAR AS I CAN GET
fb.auth.getSession()
fb.set_status('Checking out StackOverFlow.com')

When I get to the login() call, however, pyfacebook tries to open lynx so I can login to Facebook 'via the web' -- this is, obviously, not going to work for me because the system is supposed to be automated ... I've been looking, but can't find out how I can keep this all working with the script and not having to login via a web browser.

Any ideas?

Community
  • 1
  • 1
thornomad
  • 6,707
  • 10
  • 53
  • 78

2 Answers2

2

In the definition of login, particularly in the docstring, it appears as though the intended behavior is to open up a browser in order to have you log in that way.

def login(self, popup=False):
    """Open a web browser telling the user to login to Facebook."""
    import webbrowser
    webbrowser.open(self.get_login_url(popup=popup))

Looking at the facebook page User:PyFacebook_Tutorial that you linked, it looks like the example with login is a "Desktop Applications" example. You want to follow the "Web Applications" section. I'd encourage you to simply press ahead with the tutorial there.

Ewan Todd
  • 7,315
  • 26
  • 33
  • There is an explanation further down at: http://wiki.developers.facebook.com/index.php/User:PyFacebook_Tutorial#Adding_content_to_a_profile_page_without_user_interaction.2Flogin ... however, a note says that the method is currently 'overkill' ... was hoping someone had the experience to share that may help out ... otherwise, will try that route. – thornomad Dec 15 '09 at 15:02
  • The comment is that the "infinite session key trick" is overkill. I would still be inclined to follow the tutorial first, just to make it work. You can follow the session key advice after you have gained a feel for the API. – Ewan Todd Dec 15 '09 at 15:13
  • 1
    Haven't managed to post yet, but this was helpful: http://www.emcro.com/blog/2009/01/facebook-infinite-session-keys-no-more/ – thornomad Dec 15 '09 at 15:43
0

If you want to login to your facebook profile page I have managed to do it with this script:

Save this file as fb_login.py and in the same folder create a file fb_test.html

I successfully logged in as you can prove by viewing on your browser the fb_test.html or searching for your name in the plain text.

Does anyone knows how to login with simple Authedication credentials and not with SECRET AND API key that you need to make application?

import urllib, urllib2, cookielib

user = 'put_your_mail_here'
passwd = 'put_your_password_here'

file = './fb_test.html'
url_login = "https://login.facebook.com/login.php?"
url_action = "https://login.facebook.com/login.php?login_attempt=1"
url_topic = "http://www.facebook.com/profile.php?id=___put_your_profile_Number_here"
url_index = "https://login.facebook.com/login.php?"

def login(user, password, url_action):
    cj = cookielib.LWPCookieJar()
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())
    urllib2.install_opener(opener)
    opener.addheaders=[('Content-Type','application/x-www-form-urlencoded'),('Connection','keep-alive'),('User-Agent','Mozilla/5.0')]
    params = urllib.urlencode({'action':url_action , 'referer':url_index, 'email':user, 'pass':passwd, 
                                  'loginTrue':"login"})

f = opener.open(url_action, params)
    f.close()
    f = opener.open(url_action, params)
    f.close()
    return opener

def get_source_code( opener, url_x ):
    f = opener.open(url_x)
    data = f.read()
    print type(data)
    f.close()
    return data

def keep_log( data, file ):
    f = open(file, 'w')
    f.write(data)
    f.close()

opener = login(user, passwd, url_action)
src_code = get_source_code(opener, url_topic)
keep_log(src_code, file)
print src_code
limitcracker
  • 2,208
  • 3
  • 24
  • 23