All the stuff that bothers software developer in his everyday tasks.
Jan 10, 2008
Another 5 minutes with C# to post to Google Blogs (Blogger.com) using Google Blogs SDK
// Create service to be used for queries Service service = new Service ( "blogger" , "blogger-sample" ); AtomEntry createdEntry = new AtomEntry (); // Create credentials you should add your blog username // and password here service.Credentials = new GDataCredentials ( "your_username" , "your_password" ); // As I only have one blog to speed up - I am taking it, // You can extend here in order to allow the user to // select from mutliple |
Uri firstBlogUri = GetFirstBlogUri(service);
// If we have the Uri - it's time to actually post.
if (firstBlogUri != null )
{
// Create FeedQuery and assign Uri.
FeedQuery query = new FeedQuery ();
query.Uri = firstBlogUri;
// Create Atom Feed from that query.
AtomFeed feed = service.Query(query);
// Create atom entry for the new post
AtomEntry newPost = new AtomEntry ();
// Fill in the post information
// You need to have a control named richTextBox2 which will
// contain the post body and a textBox1 control which will
// contain the post title.
newPost.Title.Text = this .textBox1.Text;
newPost.Content = new AtomContent ();
newPost.Content.Content = richTextBox2.Text;
newPost.Content.Type = "html" ;
newPost.Authors.Add( new AtomPerson ());
newPost.Authors[0].Name = "Pavel Donchev" ;
// Try to publish and report success / fail:
try
{
createdEntry = service.Insert(query.Uri, newPost);
}
catch ( Exception ex)
{
MessageBox .Show(ex.Message);
}
if (createdEntry != null )
{
MessageBox .Show(newPost.Title.Text + " posted successfully!" );
}
}
There is also a helper function to retrieve the blog Uri:
Uri GetFirstBlogUri( Service service)
{
FeedQuery query = new FeedQuery ();
// Retrieving a list of blogs
query.Uri = new Uri ( "http://www.blogger.com/feeds/default/blogs" );
AtomFeed feed = null ;
feed = service.Query(query);
AtomEntry entry = feed.Entries[0];
for ( int i = 0; i < entry.Links.Count; i++)
{
if (entry.Links[i].Rel.Equals( "http://schemas.google.com/g/2005#post" ))
{
return new Uri (entry.Links[i].HRef.ToString());
}
}
return null ;
}
Jan 8, 2008
5 minutes C# code to help you blog source ...
As I was unabled to find a reliable editor to convert my code samples to html, I decided to write one. You need two richTextBox components, named richTextBox1 and richTextBox2 and a Button, named button1. Here is the code you need to put into the button1 click event handler: private void button1_Click(object sender, EventArgs e) { string oldColor = "-"; string newColor = string.Empty; StringBuilder strHtml = new StringBuilder(); for (int i = 0; i < this.richTextBox1.Text.Length; i++) { richTextBox1.Select(i, 1); |
if ((newColor != oldColor) && (newColor != string.Empty))
{
if (!oldColor.Equals("-"))
{
strHtml.Append("");
}
strHtml.Append(" + newColor + ";\">");
oldColor = newColor;
}
strHtml.Append(richTextBox1.SelectedText.Replace("<", "<").Replace(">", ">"));
Application.DoEvents();
}
strHtml.Append("");
StringBuilder sbNew = new StringBuilder();
sbNew.Append(strHtml.ToString().Replace(" ", " "));
richTextBox2.AppendText(sbNew.ToString());
}
PLEASE NOTE: this code was written in 5 or 10 minutes, it doesn't pretend to be either clean or readable, it simply works. If you want - you can take it, optimize it, use it or whatever, just have in mind it wasn't written carefully ;).
Етикети:
.NET,
.NET C# technologies,
C#,
Google Blogs API,
Windows Forms
Subscribe to:
Posts (Atom)