C# Post/Add BlogPost Blogger.com

Download GData.Client https://code.google.com/p/google-gdata/downloads/list



using System;
using Google.GData.Client;

namespace BloggerAPI
{
    public static class Blogger
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <param name="UserblogTitle">If null,then will select first user blog</param>
        public static bool SubmitNewPost(string username, string password, string UserblogTitle)
        {
            Service service = new Service("blogger", "blogger-example");
            service.Credentials = new GDataCredentials(username, password);
            try
            {
                Uri blogPostUri = SelectUserBlog(service, UserblogTitle);
                AtomEntry createdEntry = PostNewEntry(service, blogPostUri);
                return true;
            }
            catch
            {
                return false;
            }
        }

        static AtomEntry PostNewEntry(Service service, Uri blogPostUri)
        {
            AtomEntry createdEntry = null;
            if (blogPostUri != null)
            {
                AtomEntry newPost = new AtomEntry();
                newPost.Title.Text = "Marriage!";
                newPost.Content = new AtomContent();
                newPost.Content.Content = "<div xmlns=\"http://www.w3.org/1999/xhtml\">" +
                    "<p>Mr. Darcy has <em>proposed marriage</em> to me!</p>" +
                    "<p>He is the last man on earth I would ever desire to marry.</p>" +
                    "<p>Whatever shall I do?</p>" +
                    "</div>";
                newPost.Content.Type = "xhtml";
                createdEntry = service.Insert(blogPostUri, newPost);
            }
            return createdEntry;
        }

        static Uri SelectUserBlog(Service service, string UserblogTitle)
        {
            FeedQuery query = new FeedQuery();
            query.Uri = new Uri("http://www.blogger.com/feeds/default/blogs");
            AtomFeed feed = service.Query(query);
            Uri blogPostUri = null;
            if (feed != null)
                foreach (AtomEntry entry in feed.Entries)
                    if (UserblogTitle == entry.Title.Text || UserblogTitle == null)
                    {
                        for (int i = 0; i < entry.Links.Count; i++)
                            if (entry.Links[i].Rel.Equals("http://schemas.google.com/g/2005#post"))
                                blogPostUri = new Uri(entry.Links[i].HRef.ToString());
                        return blogPostUri;
                    }
            return blogPostUri;
        }
    }
}

2 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. This comment has been removed by a blog administrator.

    ReplyDelete

Note: only a member of this blog may post a comment.