Thursday, February 04, 2010

Facebook Stream_Get, Offline_Access and getFilters Not Working

I am not sure whether I am being stupid, the Facebook API documentation is really bad or the Facebook API just doesn't really work very well, but if you want to display a user's own profile feed on a page (eg, your own feed on your homepage) here are some pointers: (I do not guarantee any of this information is up to date or even correct.)

o) Setup your application on Facebook, it may be easiest to use the sample/example code they give you to start your project. Fill in the Canvas Page URL and point the Canvas Callback URL at your own domain URL. Then visit your Canvas Page URL.

i) To be able to have a page request restricted information from Facebook while the user is not logged in on a particular machine you need to request off line access, you can do this in these two ways, among others:
<fb:prompt-permission perms="read_stream,offline_access">
Grant
</fb:prompt-permission>
<script type="text/javascript">
FB.Connect.showPermissionDialog('read_stream,offline_access', ondone);
function ondone(a){ alert(a); }
</script>
ii) You then need to grab a persistent session key, in PHP this is available like so:
$key = $facebook->api_client->session_key
$key = $_POST[fb_sig_session_key]
iii) Once you've got this key you (or your script) can "become" the user like so:
$facebook->set_user($user_id, $key, 0)
iv) You can then get an array of streams using stream.get:
$streams = $facebook->api_client->stream_get($user_id, $user_id...)
v) I have not worked out how to "filter" stream_get to return just posts, or links, etc. getFilters will return a list of filters, but I have not had any success in changing the return value of stream_get, at all. There are also apps on the filter that is returned that I am sure I have not installed, Doodle Jump for example, wtf?

The $stream[posts][x][type] value in my streams reflects the following:

11 - create/join? group
46 - post
56 - wall post
80 - link
237 - flickr
none - profile pic, note

Changing the content type of my page rid it of some nasty characters:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
Remember that PHP's htmlentities() can also take a charset.

See the result at http://kitten-x.com/#facebook.

The ridiculous thing is that you can save an awful lot of time and effort by using the Facebook RSS feeds, such as:
http://www.facebook.com/feeds/share_posts.php?id=userid&viewer=userid&key=akey&format=rss20
To get your key (and userid) go to your notifications page and click on the "Via RSS" link, you can then change "notifications" in the URL to "share_posts", "status", etc.

2 comments:

Unknown said...

congrats!
how du you manage to include the number of comments on you site in the facebook part?
http://kitten-x.com/#facebook
after trying nearly 20 hours it's time to give up and ask for some help ;-)

Jamie said...

If you do a print_r on the response you will see everything available, I think comments is in:

$stream[posts][x][comments][count]