Sunday, January 11, 2015

Publish Posts on Blogger.com

The next posts will be about the blogging portal Blogger.com, on which for example this blog is running. For the access via C# we need the Blogger Data API, as is described in this documentation. Here we use version 2, but there is already a (beta) version 3, which I will probably describe later.
In today's post I want to show, how to create and publish posts with C#.
For this we first have to download the Google_Data_API_Setup from this link and install it. Then we add a reference to Google.GData.Client.dll to our project, which is in my case located in the folder C:\Program Files (x86)\Google\Google Data API SDK\Redist, and integrate it to our project via using Google.GData.Client; 
Now we can log on to Blogger:

            Service service = new Service("blogger", "");
            service.Credentials = new GDataCredentials(Email, Password);
            GDataGAuthRequestFactory factory = (GDataGAuthRequestFactory)service.RequestFactory;
            factory.AccountType = "GOOGLE";

Posts are created in the atom format, with these lines of code we create posts with title "Test" and content "Testpost":

            AtomEntry newPost = new AtomEntry();
            newPost.Title.Text = "Test";
            newPost.Content = new AtomContent();
            newPost.Content.Content = "Testpost";
            newPost.Content.Type = "xhtml";
            // newPost.IsDraft = true;

The attributes should be self explaining, via IsDraft we set whether the post should be a draft.
Finally we have to publish the post, for this we send the post to the blog feed, which is done like this:


            Uri blogFeedUri = new Uri("http://www.blogger.com/feeds/" + blogID + "/posts/default");
            AtomEntry createdEntry = service.Insert(blogFeedUri, newPost);

The complete code looks like this:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using Google.GData.Client;

namespace BloggerCreatePosts
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            Service service = new Service("blogger", "");
            service.Credentials = new GDataCredentials(Email, Password);
            GDataGAuthRequestFactory factory = (GDataGAuthRequestFactory)service.RequestFactory;
            factory.AccountType = "GOOGLE";

            AtomEntry newPost = new AtomEntry();
            newPost.Title.Text = "Test";
            newPost.Content = new AtomContent();
            newPost.Content.Content = "Testpost";
            newPost.Content.Type = "xhtml";
            // newPost.IsDraft = true;


            Uri blogFeedUri = new Uri("http://www.blogger.com/feeds/" + blogID + "/posts/default");
            AtomEntry createdEntry = service.Insert(blogFeedUri, newPost);
        }
    }
}

10 comments:

  1. Not Work, AtomEntry createdEntry = service.Insert(blogFeedUri, newPost); >> InvalidCredentialsException was unhandled

    ReplyDelete
  2. Hey, that means your credentials (username or password) are invalid. You have to enter exactly the same as when you log in in the browser, so your email address and password!

    ReplyDelete
  3. Dear friend,
    Could you please let me know, what is the blogID in " Uri blogFeedUri = new Uri("http://www.blogger.com/feeds/" + blogID + "/posts/default");"
    and how to get the value of blogID?

    ReplyDelete
  4. I got exception with below code:
    Service service = new Service("blogger", "");,
    but with out passing the strings in service that exception did not occured.
    exception is: Execution of authentication request returned unexpected result: 404

    ReplyDelete
  5. Your code returns authentication request returned unexpected result: 404

    ReplyDelete
  6. Please update your code as soon as posible

    ReplyDelete
  7. Hey guys, you're right, sorry the late replay!
    This code is deprecated, you have to use API version 3.0.
    Sitting on a post right now, should be out tmw!

    ReplyDelete
    Replies
    1. Have you created newer version of code? Please post if you are done.

      Delete
  8. Ok, sorry, as always it took longer as promised, but finally the new version is online! http://csharp-tricks-en.blogspot.de/2015/08/publish-posts-on-bloggercom-api-v30.html - correctly working with the API 3.0. Hope you like it!

    ReplyDelete
  9. Thank you so much. I'll check this over the weekend :)

    ReplyDelete