.NET GoogleReader API

I received some emails last night requesting the source for the console app (download here) that I am using to extract my feeds from google reader, so I figured I would write a quick post about it.  Just to be clear, as far as I know the API is still unofficial and I have no idea when or if google plans on making it public.  So it goes without saying that it could go away, or change dramatically at anytime.  But if you are still interested, here is what you need to know.

The best resources I could find on how the API works are

Also, there is a project setup on codeplex that is similar to what I have done.  It wasn’t exactly what I was looking for, so I passed it up, but you might also find that helpful as well.

Google’s API works by first acquiring a authentication token from google’s ClientLogin url.  Then to access your google reader items, you just attach the authentication token to your requests via a cookie.  And that is pretty much it.  pryfeed has a good description of the API’s endpoints and what parameters they accept and require.  Here is a quick example …

To get my last ten starred feeds I would do the following …

And that’s it.  All of the requests to http://www.google.com/reader/atom/… return an atom feed.  Which you can easily parse using .NET’s System.ServiceModel.Syndication.SyndicationFeed’s class.  Once the feed is parsed, you can do what ever processing you need.

In addition to the atom endpoint, there is another url (http://www.google.com/reader/api/0 …) that you can use to do things like …

  • add a label to a feed
  • subscribe/unsubscribe to a feed
  • edit tags and folders
  • list subscriptions/tags

Again, to use this endpoint, you will issue a POST/GET (depending upon the action) and tag along you authentication token.  The commands above fall into either the ‘list’ or ‘edit’ category.  ‘list’ commands return either json or xml (you get to pick).  For my application, I went with xml and used XDocument to select out the information I required.  Here is a quick example of what it takes to get a list of all the feeds I am subscribed to …

Here is a small fragment of what the resulting XML looks like …

<object>
    <list name="subscriptions">
        <object>
            <string name="id">feed/http://feeds.feedburner.com/keithelder</string>
            <string name="title">*Words of Wisdom From The Elder</string>
            <list name="categories"/>
            <string name="sortid">0F38F30A</string>
            <number name="firstitemmsec">1229796058813</number>
        </object>
        <object>
            <string name="id">feed/http://blog.dmbcllc.com/feed/</string>
            <string name="title">.NET Answers</string>
            <list name="categories"/>
            <string name="sortid">E2E8967B</string>
            <number name="firstitemmsec">1231167570178</number>
        </object>
    </list>
</object>

 

So, let me again say the API is not official.  But if this is something you could use, here is the download and below is a brief description on how my .NET Reader object works.

 

Create the Reader object

string email = "youremail@yourdomain.com";
string password = "yourpassword";
string client = "my custom reader";

using (Reader reader = Reader.CreateReader(email, password, client))
{
    //    do stuff with feed here
}

 

Fetch the list of feeds you are subscribed to

string email = "youremail@yourdomain.com";
string password = "yourpassword";
string client = "my custom reader";

using (Reader reader = Reader.CreateReader(email, password, client))
{
    //  get the list of feeds you are subscribed to
    XDocument response = XDocument.Parse(reader.GetSubscriptions());

    //  extract the feeds url from the response XML
    var feeds =
        //  xpath out the feeds url
        from f in response.XPathSelectElements("//list[@name='subscriptions']/object/string[@name='id']")
        select f.Value.Replace("feed/http", "http");

    //  print the urls ...
    foreach (string feed in feeds)
    {
        Console.WriteLine(feed);
    }
}

 

Get the last 5 posts for every feed you are subscribed to

string email = "youremail@yourdomain.com";
string password = "yourpassword";
string client = "my custom reader";

using (Reader reader = Reader.CreateReader(email, password, client))
{
    //  get the list of feeds you are subscribed to
    XDocument response = XDocument.Parse(reader.GetSubscriptions());

    //  extract the feeds url from the response XML
    var feeds =
        //  xpath out the feeds url
        from f in response.XPathSelectElements("//list[@name='subscriptions']/object/string[@name='id']")
        select f.Value.Replace("feed/http", "http");

    //  print the urls ...
    foreach (string feed in feeds)
    {
        //  load the feed (requesting the last 5 most recent posts)
        SyndicationFeed syndicationFeed = reader.GetFeed(feed, 5);

        //  loop through the last 5 posts, printing the titles and the date it
        //  was published on
        foreach (SyndicationItem item in syndicationFeed.Items)
        {
            //  do something with the posts title and published date
            string postTitle = item.Title.Text;
            DateTime postPublishDate = item.PublishDate.DateTime;
        }
    }
}

 

Get all of posts that you have marked as starred

string email = "youremail@yourdomain.com";
string password = "yourpassword";
string client = "my custom reader";

using (Reader reader = Reader.CreateReader(email, password, client))
{
    foreach (SyndicationItem item in reader.StarredFeed(10).Items)
    {
        //  do something with the post ...
    }
}

 

That’s it.  Enjoy!


TrackBack

TrackBack URL for this entry:
http://mattberseth.com/blog-mt/mt-tb.fcgi/162

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)