пятница, 19 марта 2010 г.

Developing Facebook applications in C# with ASP.NET


 I mentioned previously I've created my first Facebook (FB) application (which is still under development) but I've learned a lot along the way. I haven't found the Facebook documentation very useful at all and have gleened a lot through Google, the Facebook developer group and simply tinkering with things. As a result I've put together some steps I think could help save you some time if you're looking to write a Facebook application using ASP.NET and C#. Btw, you'll get no help from facebook.com regarding ASP.NET development.
One thing that took me awhile was understanding how an FB application appears on your profile page. I eventually realized that FBML can be used to push markup onto a users profile page. This block of markup can be set at the time you create a new application though the first time around, if you're like me, this may see a bit confusing keep in mind I'm new new to FB itself so a lot of this was confusing. From the FB toolkit you can also use the method setFBML to programmatically set this block of markup. Needless, to say being used to MSDN documentation FB doc leaves a lot to be desired. That said there are a few tools you can use to play with the API as well as sample your FBML markup.


Facebook start app screenshot
At any rate, here are the steps I followed to get my application working. Note, the project includes support for Microsoft AJAX so you'll want to have that installed as well although you could just tweak the .aspx and web.config files and use it without AJAX. The screenshot illustrates what you should get when your done. NOTE: In the following steps several links won't work unless you have an FB account.
UPDATE: I've created a new page on my wiki with updated steps and a VS.NET starter kit to make this much easier.


  1. Sign up for a Facebook account
  2. Add the Developer application to your account (you need an account for that link to work)
  3. Click Developer application from the left column and select click Set Up New Application
  4. Enter the Application Name and expand Optional Fields
  5. Under Optional Fields

    1. Set your callback URL http://localhost... will work just fine for development
    2. Select "Use iframe"
    3. Click Yes for "Can your application be added on FaceBook"
  6. Under Installation Options

    1. Check "Developer Mode"
    2. Under Site Nav use the same URL from Canvas Page URL (see Optional Fields)
    3. If you use my template set Edit URL to your Canvas Page URL + /settings
  7. Click Save then from the Developers Application browse to My Applications
  8. Click View About Page then click Add Application. This will allow you to easily work with your application during development.
  9. Download the Facebook Developer Toolkit from Microsoft
  10. Download my C# ASP.NET app to get you started or generate the boilerplate code for handling login (sorry I don't do VB.NET)
  11. You'll need to add a reference to the Facebook assemby to this project as I didn't want to include it as it being updated frequently right now
  12. Open Default.aspx.cs and fill in FACEBOOK_APPKEY and FACEBOOK_SECRET from your application's page on Facebook.
  13. Fire up the browser and hit your http://apps.facebook.com/ and you're off!
Please let me know what problems you have or let me know if these steps need tweaking.
Was this helpful?

Комментариев нет: