Facebook is fairly easy for a developer to pull data from. It turns out that Facebook has built a set of APIs for developers available at graph.facebook.com.
A developer needs to generate an access key by bringing up a special facebook login page and afterwards they can simply go to urls in their web browser like so:
https://graph.facebook.com/SOMEUSERID?access_token=FJKLDSJFKLDSJSOMETHINGSECRET
This will dump out some data about the “SOMEUSER” — it looks a bit like this (this is a public profile of a random user so I messed with the names and numbers) — other URLS have MUCH more information associated with them:
{ "id": "5158744593", "name": "Nfsajkfldaf dsjklf", "first_name": "FJKLSsdf", "last_name": "Sfjkdls", "link": "http://www.facebook.com/sjfklajlsf.sjfklds", "username": "fjklsdfjksdlfsd", "gender": "female", "locale": "en_US", "updated_time": "2012-11-27T14:32:09+0000" }
As any developer knows, this strange looking text is called JSON and can be pulled apart by a computer program and easily analyzed, logged or processed. There’s a tool for exploring the Facebook API that makes it easy to generate the required access_token property.
https://developers.facebook.com/tools/explorer
It’s really fascinating. I HIGHLY recommend exploring it. When you’re playing with the API try this: http://graph.facebook.com/me?metadata=1
The metadata flag will show you all the possible sub-properties associated with your facebook record. So for instance if you said http://graph.facebook.com/me/feed you’d see a dump of all the data in your feed. What’s cool is that this is ALL the available data. Kinda de-mystifies what is public and private.
One thing that’s not that well documented (because it’s new) is how to get dumps for the admin sections of fan pages.
Why would you want to do this? Well let me give you a real life example. The company I currently work for (which shall remain nameless) has a fan page on Facebook. Lots of our users come to that fan page and use the little message button to get answer questions about purchasing our product. It’s hard to manage these posts in Facebook because there’s no good way to organize them. So what to do?
Well it turns out that if you have the right access token then you can download the fan inbox just like you downloaded the user dump above. In this case you’d do it by going to the web address: https://graph.facebook.com/fanpagename/conversations?access_token=AJKLFDJLSDKFJDSKL
The challenging part here is that the access token you need is different than the one you use to get access to your personal information. You need the PAGE access token. How do you get that?
Well you can start by getting a personal access token. I’m not talking about how to do that in this post because there are plenty of docs around the web that tell how to do that. If it’s hard for you and you just want to test you can go to that graph explorer I mentioned earlier and that’ll let you generate a personal access token.
Once you have a personal access token you then have to retrieve the page access token. To do that you need to go to:
https://graph.facebook.com/me/accounts?access_token=USER_ACCESS_TOKEN
Now this url will take you to a page that looks a bit like this:
{
"data": [
{
"name": "PAGE_TITLE",
"access_token": "PAGE_ACCESS_TOKEN",
"category": "PAGE_CATEGORY",
"id": "PAGE_ID"
},
{
"name": "PAGE_TITLE",
"access_token": "PAGE_ACCESS_TOKEN",
"category": "PAGE_CATEGORY",
"id": "PAGE_ID"
},
...
]
}
That access token you see there is the page access token and it is the one you need in order to access the conversations feed. There’s one other thing. When you’re logging into Facebook with your personal access token you’ll need to request 2 permissions — How do you do that? Ok fine…. In JavaScript it’s like so:
function login() {
FB.login(function(response) {
if (response.authResponse) {
// connected
} else {
// cancelled
}
}, {scope: 'manage_pages,manage_page_inbox'});
}
That scope object being passed in is requesting the manage_pages and manage_page_inbox privilege. That’s how it’s done. A lot of this stuff is taken right off the Facebook docs. But I did find that manage_page_inbox permission wasn’t documented anywhere and there wasn’t a lot of good info on how to request multiple permissions on login or whether you had to do that when getting your personal token or do it when you get the page token.
A few hoops to jump through but well worth it. Evidence I have at the moment seems to show that people like leaving valuable messages in that fan page inbox.