--> del.icio.us - Automatic Blog Post (Tagging Update) - Impersonation Failure

del.icio.us - Automatic Blog Post (Tagging Update)

I've updated the HttpHandler for Community Server del.icio.us daily blog post support I posted yesterday to include post tagging. The updated del.icio.us.ashx can be downloaded from here and the code is also included at the bottom of the post.

Despite the fact that the metaweblog.newPost API used by del.icio.us to create a new post supports post categories they choose to ignore this and instead make use of the Movable Type API mt.setPostCategories to set the categories after the post were made. Strange...

The del.cio.us "daily blog post" functionality also don't allow mutiple tags so you're restricted to a single tag per post. And instead of a string tag they also expect you to enter a category id so you'll need to get this from your blog first. (One way to get this is to open your blog control panel, browse to the tag/key words section and just hover over the desired tag. The categoryId in the querystring is the value we're interested in)

Besides all that, still a pretty useful feature. Below the code for the del.icio.us httphandler. Drop into blogs folder to enable "daily blog post" support. Download here.

A step-by-step guide for setting this up in your del.icio.us account on "how now, brownpau?" The out_url would be http://%3cyourcs/blogs/del.icio.us.ashx ie. http://dotnet.org.za/blogs/del.icio.us.ashx.

<%@ WebHandler Language="C#" Debug="true" Class="CommunityServer.Blogs.Components.Delicious" %>
using System;
using System.Collections;
using System.Configuration;
using System.IO;
using System.Text.RegularExpressions;
using System.Web;
using CommunityServer;
using CommunityServer.Blogs.Components;
using CommunityServer.Components;
using CookComputing.XmlRpc;
 
namespace CommunityServer.Blogs.Components
{
    // http://www.sixapart.com/developers/xmlrpc/movable_type_api/mtsetpostcategories.html    
    [XmlRpcMissingMapping(MappingAction.Ignore)]
    public struct Category
    {
        public int categoryId;
        public object isPrimary;
    }
 
    public interface IDelicious
    {
        [XmlRpcMethod("metaWeblog.newPost",
            Description = "Makes a new post to a designated blog using the "
            + "MetaWeblog API. Returns postid as a string.")]
        string newPost(
            string blogid,
            string username,
            string password,
            MetaWeblog.Post post,
            object publish);
 
        [XmlRpcMethod("mt.setPostCategories",
            Description = "Sets the categories for a post.")]
        bool setPostCategories(
            int     postId,
            string username,
            string password,
            Category[] categories);
    }
 
    /// <summary>    
    /// Generalized metablog.newPost endpoint for del.icio.us    
    /// </summary>        
    public class Delicious : XmlRpcService, IDelicious
    {
        public string newPost(
            string blogid,
            string username,
            string password,
            MetaWeblog.Post post,
            object publish)
        {
            IMetaWeblog metablog = new MetaWeblog();
            bool p = Convert.ToBoolean(publish);
            return metablog.newPost(blogid, username, password, post, p);
        }
 
        public bool setPostCategories(
            int postid,
            string username,
            string password,
            Category[] categories)
        {
            if(ValidateUser(username,password)) {
 
                WeblogPost wp = WeblogPosts.GetPost(postid, false, false, false);
                
                if(wp == null)
                    throw new XmlRpcFaultException(0, "Post not found");
                
                Weblog weblog = Weblogs.GetWeblog(wp.SectionID,true);
                
                // No exception, redirect
                Permissions.AccessCheck(weblog,Permission.Post, CSContext.Current.User);
 
                string[] splits = 
                    Array.ConvertAll<Category, string>(categories, 
                    delegate(Category c) {                        
                        try{                        
                            return PostCategories.GetCategory(c.categoryId, wp.SectionID).Name;                        
                        }
                        // CategoryId don't exist just ignore it.
                        catch (NullReferenceException){                            
                            return null;
                        }
                    });
                                
                wp.Categories = splits;                                
                WeblogPosts.Update(wp);
 
                return true;
            }
 
            throw new XmlRpcFaultException(0,"Invalid username/password");            
        }    
    
        private bool ValidateUser(string username, string password)
        {
            if(String.IsNullOrEmpty(username))
                throw new XmlRpcFaultException(0, "No username");
            
            try    {
                User u = new User();
                u.Username = username;
                u.Password = password;
                CommunityServer.Components.LoginUserStatus status = Users.ValidUser(u);
                if(status == CommunityServer.Components.LoginUserStatus.Success) {                    
                    CSContext.Current.User = Users.GetUser(-1,u.Username,false,false);
                    return true;
                }
                
                return false;
            }
            catch{
                throw new XmlRpcFaultException(0, "Membership unavailable");
            }            
        }
    }
}

Comments

# Impersonation Failure : del.icio.us - Automatic Blog Post said:

PingBack from http://dotnet.org.za/armand/archive/2006/11/24/DailyLinkBlog.aspx

Saturday, November 25, 2006 12:53 PM
# markn said:

You should never swallow exceptions. In ValidateUser for instance, you never know if the user is invalid because they are not actually valid or because some or other error occurred attempting to determine the validity of the user. In setPostCategories, you should catch a specific exception before returning null.

Sunday, November 26, 2006 12:28 PM
# Armand du Plessis said:

Yep, thanks Mark. I've updated the code with more accurate error descriptions.

ValidateUser was from the original metablog.ashx (Private) but I agree, if the membership provider throws you might want to let the consumer know that it's not an invalid user but that the system is experiencing serious issues.

Guilty as charged with GetCategory, I didn't have the CS SDK on this machine so were unsure what exception/if any would come back if the category couldn't be found and the MT spec stated to ignore non-existant categories hence the lazy catch. Updated.

Other exceptions now bubbled and wrapped into generic XmlRpcFaultExceptions by the CookComputing.XmlRpc library.

I appreciate your comments.

Monday, November 27, 2006 8:32 AM
# Community Server Daily News said:

news of the day a grab bag for what's happening in Community Server Tim Laughlin will be migrating an

Monday, November 27, 2006 10:57 PM
# Daily News List Blog said:

Armand du Plessis adds more "deliciousness" to his cadre of recent MetaBlog API efforts. Del.icio.us

Monday, November 27, 2006 11:13 PM
# Community Server Bits said:

Armand du Plessis adds more "deliciousness" to his cadre of recent MetaBlog API efforts. Del.icio.us

Monday, March 12, 2007 12:05 PM
# Tim Laughlin's Everything VB.NET Blog said:

So recently joined de.icio.us, not sure why it took me so long. I am still playing with the bubble wrap,

Saturday, June 02, 2007 8:23 PM
# fractalnavel said:

i'm going to try daily auto-posting of my del.icio.us links for a while, see how that goes. expect lots

Thursday, August 16, 2007 1:26 AM
# Kenneth Auchenberg said:

769789 (tags: del.icio.us CommunityServer )

Saturday, January 05, 2008 4:19 PM
# Kenneth Auchenberg said:

769789 (tags: del.icio.us CommunityServer )

Saturday, January 05, 2008 11:09 PM
# Kenneth Auchenberg said:

769789 (tags: del.icio.us CommunityServer ) Ergo (tags: wpf tablet )

Sunday, January 06, 2008 2:20 PM
# Kenneth Auchenberg said:

Hej folkens Efter lang tids kamp er det endeligt lykkedes mig at s&#230;tte del.icio.us op til at poste

Monday, January 07, 2008 6:07 PM
# automate blog posting said:

Pingback from  automate blog posting

Tuesday, April 15, 2008 4:40 AM