Tuesday, July 12, 2011

C# Client Application to add new posts to blogspot.com (automated stuffs)

lang: c#

download: Google Data API 21.7mb

install the msi. normaly it contents several libraries. but for this app we need only using Google.GData.Client;
add reference to ur solution.


using System.Net;
using Google.GData.Client;


public static void Main()
{
string posttitle="How to make a cup of tea";
// body contains html
string body = "<p>sample paragraph<img src="http://www...."/><a href></a></p>";
//string[] label
string[] label={"tea","cup of tea","askamith"};

bool abc = AddPost(posttitle, body, label);

}



public static bool AddPost(string title, string bodyHTML, string[] labels)
{
Service service = new Service("blogger", "askamith-global");
service.Credentials = new GDataCredentials("your blogspot gmail-address", "password");
AtomEntry newPost = new AtomEntry();
newPost.Title.Text = title;
newPost.Content = new AtomContent();
newPost.Content.Content = bodyHTML;
newPost.Content.Type = "html";
foreach (string label in labels)
{
AtomCategory cat = new AtomCategory();
cat.Scheme = new Uri("http://www.blogger.com/atom/ns#");
cat.Term = label;
newPost.Categories.Add(cat);
}
AtomEntry response = null;
try
{
response = service.Insert(new Uri("http://www.blogger.com/feeds/xxxxxxxxxxxxx/posts/default"), newPost);
}
catch (GDataRequestException exception)
{
if (exception.ResponseString == "Blog has exceeded rate limit or otherwise requires word verification for new posts")
{
return false;
}
else
{
throw exception;
}
}
if (response == null)
{
throw new Exception("Something went wrong");
}
return true;
}



for new Uri("http://www.blogger.com/feeds/xxxxxxxxxxxxx/posts/default")

xxxxxxxxxxxxxxx= your blogger id (it will be shown in the url in adding a new post page in ur blogger dashboard)

http://www.blogger.com/post-create.g...xxxxxxxxxxxxxx

any questions regarding this. comment those bellow. its 100% working. try it. thanks

No comments: