This week we’re creating our first Facebook application for one of our clients.
It sounds fairly reasonable to want to include the latest posts from your wall from your Facebook page on your own website, and until recently, that was something that was fairly easy to do as walls and pages had the option to “Get updates via RSS” which would give you an RSS feed that you can subscribe to.
That changed when Facebook introduced the Graph API and they have seemingly been phasing out the RSS feed since to the point that when you click the link on a page where it’s been disabled you’ll get a message instead saying that the service has been disabled.
This seems to make sense, as the Graph API is much more privacy conscious and this therefore prevents people’s wall posts if they’re not friends with sufficient rights, but just seems a pain for pages, which in my opinion are meant to be public.
Anyway, we’ve struggled through the process of creating our app, specifically with C# and ASP.NET to do something seemingly simple, so I thought it worth sharing for others, how you can do this here so that everyone else can benefit too. There are plenty of guides on how to access Facebook using PHP, Ruby, Python, and pretty much everything
Firstly, you need to include Facebook C# SDK in your project, if you’re using NuGet you can search ‘Facebook’ and you’ll find it quite easily, or run:
PM> Install-Package Facebook -Version 5.4.1.0
Otherwise it’s available on codeplex here:
facebooksdk.codeplex.com
OK, once you’ve got it installed, and (I recommend) before you start writing any code there’s a few things that you need to do (and I think this information is what I’ve struggled the most to find – maybe it’s just me).
Disclaimer: What I’ve done here may not be the correct approach, but it works, and seems the easiest for me to understand and implement. Some other approaches require users to authenticate with facebook first which is not the desired result.
Create a new app
Visit:
http://developer.facebook.com/apps
This is the area for developers that allows you to manage any apps that you’ve created. You need to create a new one, this will give you some API keys etc. but ultimately allow you to create an “access_token”. This is the magic code that you need to be able to read wall posts.
Create access token
Once you’ve created your new application, you’ll need to navigate to the Graph API Explorer. There are a number of ways to reach this, the first is a ‘Useful link’ that appears when you’re editing your applications settings on the previous page, or you can find it from the developer tools. Both of these links seem buried away to me, so here’s a quick link for you:
https://developers.facebook.com/tools/explorer
When you visit the page, you’ll see a test harness that lets you see the results when you can various parts of the Graph API. We’ll make proper use of this in a little while, but firstly, change the application from the drop down list in the top right hand corner to the one you just created.
You’ll notice that Access Token is blank (or if you’ve had problems previously, it may have a value which doesn’t have the right permissions).
Click the button to “Get Access Token”. You’ll see a dialog appear of which you need to select the following permissions:
From “User Data Permissions”:
- user_status
This allows your application, using the access token, to read from yours and other people’s walls as if it were you, so it can access all walls that you have access to.
From “Extended Permissions”:
- offline_access
This allows your application to continue working even after you have logged out. In an early attempt of these, we got everything working without this permission, then after signing out of facebook and coming back to our code, we found that the access_token had expired.
Click “Get access token” and you’ll see the access token text box populated with your access token.
You’re supposedly able to do this from the Facebook C# SDK, but I couldn’t see any examples that didn’t involve user authentication in your website to do this, so I’ll say there’s an area for improvement here. Once you have the instructions above, I think this is quite quick and easy to do manually without the need to write code, so that’s my advice.
This approach allows the application that you’ve created to run as ‘you’, with access to ‘your’ wall. If you’re conscious about anyone that isn’t a friend potentially seeing the contents of your personal wall, you may want to create a new facebook account, exclusively for this purpose. In principle, as long as the user that you’re logged in as, when you generate the access token, can read from the wall that you want to display posts from, then you’ll be able to read them from your application.
Put the access token somewhere for reference, your web.config for example, and then we’re ready to start writing some code.
Graph API
The Graph API, allows you t o do anything you want/need with Facebook via a structured URL. the response containing a JSON object(s).
You can use the Graph API Explorer to try various commands and view the output, in the context of your application and the permissions that you defined when you generated your access token.
You can find a comprehensive listing of methods that you can call in the Graph API documentation on the Facebook Developer portal.
For the purpose of reading wall posts, we’re going to use the format /{user_or_id}/feed. For example, the path for mydigitalmedia’s wall on our page would be /mydigitalmediaonline/feed.
You can try entering that path into the Graph API Explorer to view the results.
Extract posts
To extract the posts for a particular wall, you can use the following few lines of code:
Facebook.Web.FacebookWebClient client = new Facebook.Web.FacebookWebClient(ConfigurationManager.AppSettings["app_token"].ToString());
//Get wall posts
var result = (IDictionary<string, object>)client.Get(ConfigurationManager.AppSettings["page_id"].ToString() + "/feed");
var posts = result.Values.First();
If you’ve not used NuGet to include Facebook in your project, you’ll need to add a reference to the Facebook.dll as well.
The above will populate posts with a dictionary of posts, each consisting of a JSON string. From here, you simply need to parse the text however you like. I’m not going to blog about this here, as I think it’s a different topic (which I may cover separately another time), but some tips may be to use a JSONSerialiser to deserialise the script into objects, which is included through the Facebook SDK, though you can also use a javascript serialiser to do the same thing, as ultimately, JSON is javascript
Hopefully this post will be some help to others, and can save you the time it’s taken me in research finding how to access Facebook and the use the Graph API from C# and ASP.NET.
If you have any comments, or suggestions on how to do this better, please share them below for everyone’s benefit.