views:

57

answers:

2

I'm working on a script currently that needs to pull information down from a specific user's wall. The only problem is that it requires authentication, and the script needs to be able to run without any human interference. Unfortunately all I can find thus far tells me that I need to register an application, and then do the whole FB Connect dance to pull off what I want. Problem is that requires browser interaction, which I'm trying to avoid.

I figured I could probably just use httplib2, and login this route. I got that to work, only to find that with that method I still don't get an "access_token" in any retrievable method. If I could get that token without launching a browser, I'd be completely set. Surely people are crawling feeds and such without using FB Connect right? Is it just not possible, thus why I'm hitting so many road blocks? Open to any suggestions you all might have.

+3  A: 

What you are trying to do is not possible. You are going to have to use a browser to get an access token one way or another. You cannot collect username and passwords (a big violation of Facebook's TOS). If you need a script that runs without user interaction you will still need to use a browser to authenticate, but once you have the user's token you can use it without their direct interaction. You must request the "offline_access" permission to gain an access token that does not expire. You can save this token and then use it for however long you need.

Nathan Totten
Thanks for the information. I'm not collecting username/passwords to be clear though. It's our companies username/password. Just putting together a script that'll run nightly via cron or something. Looks like I'll be going a different route. Thanks again :)
f4nt
So if that is the case, just do the authentication one time with offline_access and just save the access token and use it in your script. The token will never expire unless the account removes the application.
Nathan Totten
@f4nt I think your going to need a browser. That said, you can automate a browser without being present. Check out Watir (ruby lib) for an excellent browser automation. Watir has versions for n windows, mac, and linux and works with safari, chrome, IE, and firefox. I do everything in python except browser automation, which I do with ruby/Watir.
Steven Rumbalski
@f4nt Nevermind. Looks like Nathan really knows how to do it.
Steven Rumbalski
what is the browser doing that can't be done with something like mechanize?
Corey Goldberg
You could do it with the browser, but that isn't really the right way to do it. For him, he is just doing this with his own username and password, but if it were other people's u/p that would be a violation of Facebook's TOS.
Nathan Totten
@Corey You get an access token via their javascript OAuth nonsense I believe. The purpose is to grab that access token, and then leverage their API. I can get in with httplib2 and stuff, but that access token never becomes obtainable for me. Then again, I haven't fought it *that* hard yet :)
f4nt
A: 

I've done this with my own account before using mechanize. You can log in to Facebook using something like the below and then just follow the links to where you want to go. This will print out the contents of you news feed.

#!/usr/bin/env python

import mechanize

browser = mechanize.Browser()
browser.set_handle_robots(false)
cookies = mechanize.CookieJar()
browser.set_cookiejar()
browser.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/534.7 (KHTML, like Gecko) Chrome/7.0.517.41 Safari/534.7')]

browser.select_form(nr=0)
browser.form['email'] = 'YOUR_EMAIL_ADDRESS'
browser.form['pass'] = 'YOUR_PASSWORD'
response = browser.submit()
print response.read()
EverythingZen