Pulling a Facebook RSS Feed via .NET
I wanted to add my Facebook status to my other blog which is running Community Server 2.0. All I needed to do was write a user control to pull the RSS feed and display the text. This seemed easy enough, but it turned out to be a little tricky.
First I tried to use the XmlDocument class and load the RSS URL directly. That complained about a missing DTD which was confusing since the URL works fine in a browser. After some debugging, I discovered that Facebook sniffs your User Agent. Since the XmlDocument was sending an unknown user agent field, Facebook was replying with this page.
I changed my tactics a bit and used the HttpWebRequest object which allows me to specify the user agent field. After that it was relatively simple to load the response stream into an XmlDocument, use an XPath query to find the node I wanted, and load the status text into a literal control. Feel free to copy this code if you have run into a similar problem.
<%@ Import namespace="System" %>
<%@ Import namespace="System.IO" %>
<%@ Import namespace="System.Net" %>
<%@ Import namespace="System.Xml" %>
<%@ Import namespace="System.Web.UI.HtmlControls" %>
<%@ Import namespace="System.Web.UI.WebControls" %>
<%@ Import namespace="System.Web" %>
<%@ OutputCache Duration="600" VaryByParam="none" %>
<%@ Control Language="vb" AutoEventWireup="true" EnableViewState="false" Debug="false" %><script runat="server" language="vb">
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ltlFacebook.Text = ""
Dim sReader as StreamReader
Dim resp as HttpWebResponse
Try
Dim req as HttpWebRequest = CType(WebRequest.Create("https://www.facebook.com/feeds/status.php?id=584666720&viewer=584666720&key=12cf50d10b&format=rss20"), HttpWebRequest)
req.UserAgent = "Windows-RSS-Platform/1.0 (MSIE 7.0; Windows NT 5.1)"
resp = CType(req.GetResponse(), HttpWebResponse)
sReader = new StreamReader(resp.GetResponseStream(), Encoding.UTF8)
Dim xmldoc As New XmlDocument
xmldoc.LoadXml(sReader.ReadToEnd())
Dim node as XmlNode = xmlDoc.SelectSingleNode("/rss/channel/item/title")
ltlFacebook.Text = node.InnerText
Finally
sReader.Close()
resp.Close()
End Try
End Sub
</script>
<asp:Literal id="ltlFacebook" runat="server"></asp:Literal>
Comments
- Anonymous
January 03, 2008
PingBack from http://msdnrss.thecoderblogs.com/2008/01/03/ - Anonymous
January 03, 2008
You may have noticed that there is a small new addition to the site. My current Facebook status is displayed... - Anonymous
November 14, 2008
How do you discover your 'own' RSS feed url? - Anonymous
November 14, 2008
There is an RSS feed on your Facebook Mini-Feed Status Stories page.http://www.facebook.com/minifeed.php?filter=11 - Anonymous
November 14, 2008
From facebook...While Mini-Feed is no longer available on Facebook, your profile still contains all of the interesting stories about you in the Wall tab. The Wall tab includes Wall posts that used to appear on the Wall application on your profile, as well as all of the stories that used to appear in your Mini-Feed. You can control the stories and posts on your Wall tab even more effectively now. For example, if there is a story on your Wall that you really like and want to highlight to your friends you can hover over the story in question, click the ‘edit' icon to the right of the story and then select "Make bigger." You can also customize how the story displays when it is generated. For example, if you add new photos to your Wall, you will have the option to edit the story that appears on your Wall. - Anonymous
November 14, 2008
Odd. Here's how I got that link to work: Open up a new tab and log into facebook. Without logging out, switch back to this post and click the link. When I do that, it goes to the correct page and shows the RSS feed. But if I'm not logged in then I just get the log in page and never end up at the right place.