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
blogs if he / she has.
             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 ;
       }

No comments: