Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pull RSS Feeds From Facebook Page

I need help to pull RSS feeds from a facebook page I'm using the following code but it keeps giving me an error :

string url = 
    "https://www.facebook.com/feeds/page.php?id=40796308305&format=rss20";

XmlReaderSettings settings = 
    new XmlReaderSettings
                    {
                         XmlResolver = null,
                         DtdProcessing=DtdProcessing.Parse,

                     }; 
XmlReader reader = XmlReader.Create(url,settings);

SyndicationFeed feed = SyndicationFeed.Load(reader);

foreach (var item in feed.Items)
{
    Console.WriteLine(item.Id);
    Console.WriteLine(item.Title.Text);
    Console.WriteLine(item.Summary.Text);

}

if (reader != null) reader.Close();

This code works perfectly with any blog or page rss but with Facebook rss it give an exception with the following message

The element with name 'html' and namespace 'http://www.w3.org/1999/xhtml' is not an allowed feed format.

Thanks

like image 739
Yassmeen Avatar asked Jun 09 '11 15:06

Yassmeen


People also ask

What is RSS feed in Facebook?

Instant Articles support syndication using a secure RSS feed. This feed can integrate seamlessly with your existing workflow if new stories are automatically syndicated as Instant Articles whenever you publish them from your regular content management system.

How do I get an RSS feed link?

Right click an empty space on the website you'd like an RSS feed for, then click View Page Source (the exact wording may vary depending on your browser). If searching for rss doesn't work, try atom instead. Look for an RSS URL, as you can see above, then copy it into your feed reader.

How can RSS feeds be used with social media?

When a new article is published in your feed, it will automatically be added to your Content Library. Your content library is connected to your social profiles and will post new content in real-time. This way it will help you in creating a viral content marketing strategy for your feed & help in RSS feed automation.


2 Answers

Facebook will return HTML in this instance because it doesn't like the User Agent supplied by XmlReader. Since you can't customize it, you will need a different solution to grab the feed. This should solve your problem:

var req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "GET";
req.UserAgent = "Fiddler";

var rep = req.GetResponse();
var reader = XmlReader.Create(rep.GetResponseStream());

SyndicationFeed feed = SyndicationFeed.Load(reader);

This is strictly a behavior of Facebook, but the proposed change should work equally well for other sites that are okay with your current implementation.

like image 120
AC2MO Avatar answered Sep 18 '22 22:09

AC2MO


It works when using Gregorys code above if you change the feed format to atom10 instead of rss20. Change the url:

string url = 
"https://www.facebook.com/feeds/page.php?id=40796308305&format=atom10";
like image 23
isFredrik Avatar answered Sep 21 '22 22:09

isFredrik