Blog > January 2012 > Creating our first Facebook application

Creating our first Facebook application

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.

Posted: 16/01/2012 14:23:22 by Kurt Farrar | with 1 comments
Filed under: Facebook, Microsoft, Apps


Trackback URL: http://www.mydigitalmedia.co.uk/trackback/d3d325f2-db62-40d3-9fee-97aaf6cf5243/Creating-our-first-Facebook-application.aspx

Comments
Carley
I would like to show my appreciation to the ertiwr just for rescuing me from such a challenge. After surfing throughout the search engines and meeting methods that were not beneficial, I assumed my life was gone. Existing minus the answers to the problems you have solved through your entire guide is a critical case, as well as those which may have negatively affected my career if I had not discovered your web site. The skills and kindness in handling all the things was helpful. I don't know what I would have done if I had not discovered such a point like this. I am able to at this point look forward to my future. Thanks a lot very much for your expert and result oriented help. I won't think twice to endorse your blog post to any individual who will need direction on this issue.
16/04/2012 18:56:51

MyDigitalMedia's online blog covering topics from technology and the internet, to marketing and ways to run your business more efficiently.

Our blog is lead by whatever our current experiences and projects happen to be, but if you have any particular subjects that you'd like to see here, please get in touch blog@mydigitalmedia.co.uk

Our online blog covers a wide range of subjects including:
  • Internet
  • Web design
  • Web development
  • Latest devices
  • Online services
  • Technology trends
  • Brand
  • Marketing
  • Photography
  • Business automation
  • Cutting overheads
If there is anything in particular that you'd like to see on our blog, please get in touch blog@mydigitalmedia.co.uk

Recent posts

BidSketch - A Review

An overall smarter way to generate your proposals consistently, quickly and easily.  Gives even the simplest proposal a polished and professional look and feel. Requires no technical knowledge whatsoever suiting Business Development Managers in any industry including non-IT businesses. Read our review of BidSketch.com. 



Google AdWords and Ad Performance
One of our client's asked us this week all about how they can achieve a better position within the sponsored search results for Google AdWords. We put together some rather lengthy advice and then...

Blogging to Kentico website using Word 2013 preview
Many of you may have used and experienced the benefits of Windows Live Writer, and it's a fantastic blogging tool, second to none in my opinion, and as soon as the rumours of the Windows Live brand...

Promote for £4? What’s this all about?
This week, we’ve had a number of our customers that rely heavily on Facebook for the promotion of their business as part of their marketing strategy contact us asking what the ‘Promote’ option that...

The value of effective marketing
Whilst reading the Metro on my daily commute this morning I was reading how Disney's latest film to hit the cinema (John Carter) is set to be the biggest flop of the year, with the estimated loss...

Syndication

RSS
MyDigitalMedia are a small business IT consultancy specialising in web design, and web development creating high quality bespoke web solutions, and establishing our client's brands online, without a hefty price tag. Based in Widnes, Cheshire we cover all surround areas including St. Helens, Liverpool, Warrington, and Runcorn and have a number of national clients from throughout North Wales, and as far as Plymouth. Thanks to the internet, and online conferencing, we're never too far away. View our portfolio to view examples or projects that we have undertaken to date.