Yesterday, I found a cool .NET API offered by Google. The Blogger Developer’s Guide for .NET is a cool way to dynamically link your Blogger posts within your own website.
I was a bit frustrated by the documentation (both on the website and within the .NET-generated docs included with their code), so I have decided to write my own tutorial.
Requirements
- You need to know .NET (either C# or VB.NET)
- You need to be using some version of Visual Studio. I used Visual Web Developer (Express)
Step One
First, you’re going to need to download and install the API code. Visit this page and download the MSI file. Save and run the file.
Step Two
Next, open your website (or application, etc) in Visual Studio. Pick a page where you want your blog posts to display, and paste the following code into the ASPX page (not the code-behind file).
<div id="blogger" runat="server" />
Step Three
In order to use the Google API libraries, we need to include them in our application. Under Website – Add Reference, click the Browse tab and navigate to C:\Program Files\Google\Google Data API SDK\Redist\ and include the files Google.GData.Client.dll and Google.GData.Extensions.dll. You’ll notice that these files are now added to your \Bin\ folder.
Note: Be sure to upload the \Bin\ directory when you post the code to your web server.
Step Four
After you’ve created the above HTML element (a DIV tag), open the code-behind file. We’re going to add the following code (VB.NET) so we have access to the libraries we need:
Imports Google.GData.Client Imports System.Net Imports System.Xml Imports System.IO Imports System.Text.RegularExpressions
Step Five
Finally, we’re going to add the following code to actually do the work for us:
Sub Page_Load() Handles Me.Load
Dim bloggerHtml As String = ""
Try
Dim feedQuery As FeedQuery = New FeedQuery
Dim blogId As String = "0000000000000000000"
feedQuery.Uri = New Uri("http://www.blogger.com/feeds/" + blogId + "/posts/default")
feedQuery.NumberToRetrieve = 5
Dim googleService As Service = New Service("blogger", "XXXXXXXXXX")
Dim atomFeed As AtomFeed = googleService.Query(feedQuery)
Try
bloggerHtml += "<ul>"
Dim entry As AtomEntry
For Each entry In atomFeed.Entries
bloggerHtml += "<li><a href='" & _
entry.AlternateUri.ToString & _
"' target='_blank'>" & entry.Title.Text & _
"</a></li>"
Next
bloggerHtml += "</ul>"
Catch feedEx As Exception
bloggerHtml = "<p>An error occurred while retrieving the Atom Feed from Blogger.</p>"
End Try
Catch ex As Exception
bloggerHtml = "<p><strong>Problem with Blogger API: </strong>" & ex.Message & "</p>"
End Try
blogger.InnerHtml = bloggerHtml
End Sub
This function will run as the page loads, and place HTML code inside of the DIV tag (id=”blogger”) we created in step two.
Be sure to replace the blogId variable (“0000000000000000000″) with your own Blogger id. You can find this by looking at the source code of your blog — it’s used in several of the first lines. You should also replace the “XXXXXXXXXX” with the name of your company/website/project. Honestly, I don’t know if it really matters what you put in this string… the documentation was sketchy on the details.
Your website should now display the last 5 blog posts from your Blogger account!