Posting to Blogspot using Google GData API

google gdata api tutorial

In this tutorial I will show you how to use Google GData API to Post to a Blogspot/Blogger Blog from google using a C# or .Net Application. You will see also how to upload Image to google using API and attach it to blog post and how to add tags directly from your C# Application.

First of all you have to download Google_Data_API here or download directly the libs which I have used in this tutorial here.

Ok, after you’ve downloaded Google API Libs let’s start.

1.Adding GData Libs Dependencies

UnRar libs on C:\Libs\ for example.

Create a New C# Project >> Go to Solution Explorer >> Right-Click on Project >> Add >> Reference…

google api libs gdata

Now go to Browse >> Click Browse Button >> Go to C:\Libs\Google API\Samples and select:
"Google.GData.Photos.dll" "ExecRequest" "Google.GData.Blogger.dll" "Google.GData.Client.DLL" "Google.GData.Extensions.dll" >> Add

google api dependencies blogger

Now you have to add to your code:

using Google.GData.Blogger;
using Google.GData.Client;
using Google.GData.Photos;
using Google.GData.Extensions;

And that’s all with libs, now let’s post something…

2.How to post to blogspot

A simple kind of post looks like:

	CheckForIllegalCrossThreadCalls = false;
    Service service = new Service("blogger", "googleAPIshittyName");
    service.Credentials = new GDataCredentials("[email protected]", "yourGooglePassword");
    GDataGAuthRequestFactory factory = (GDataGAuthRequestFactory)service.RequestFactory;
    factory.AccountType = "GOOGLE";

	AtomEntry post = new AtomEntry();
    post.Title.Text = "YourPostTitle";
    post.Content = new AtomContent();
    post.Content.Content = "Post Body, Descrption...";
    post.Content.Type = "xhtml";
	string tags = "tag1,tag2,tag3";
	string[] tagsS = tags.Split(',');
	foreach(string tag in tagsS)
	{
		AtomCategory cat = new AtomCategory();
		cat.Scheme = new Uri("http://www.blogger.com/atom/ns#");
		cat.Term = tag;
		post.Categories.Add(cat);
	}
	post.IsDraft = false;

	Uri blogFeedUri = new Uri("http://www.blogger.com/feeds/" + "yourBlogspotID" + "/posts/default");
    AtomEntry createdEntry = null;
    try
    {
        createdEntry = service.Insert(blogFeedUri, post);
        MessageBox.Show("Done!");
    }
    catch (GDataRequestException exception)
    {
        if (exception.ResponseString == "Blog has exceeded rate limit or otherwise requires word verification for new posts")
			MessageBox.Show("Blog has exceeded rate limit or otherwise requires word verification for new posts");
    }
    if (createdEntry == null)
        MessageBox.Show("Something went wrong, atricle was not published...\nCheck all html tags...");

And you can upload an image to google in this way:

public string uploadImageToGoogle(string path, string username, string password, string blogId)
{
    if (!System.IO.File.Exists(path))
        return "Error! Image file not found";
    ///////////////////////token session and shits...///////////
    PicasaService service = new PicasaService("HowToFix.PRO");
    service.setUserCredentials(username, password);
    /////////////////cdefault album is dropBox or something////
    Uri postUri = new Uri(PicasaQuery.CreatePicasaUri(username));

    System.IO.FileInfo fileInfo = new System.IO.FileInfo(path);
    System.IO.FileStream fileStream = fileInfo.OpenRead();

    PicasaEntry entry = (PicasaEntry)service.Insert(postUri, fileStream, "image/png", "nameOfYourFile");
    fileStream.Close();

    PhotoAccessor ac = new PhotoAccessor(entry);
    string contentUrl = entry.Media.Content.Attributes["url"] as string;

    return contentUrl;
}

Th function to upload image to google will return direct link to your image which you will use in your post like:
<img src="returnedString" />

*blogId: You can take it from Blogger. Login to blogger >> Go to Desired Blog and link looks like:
https://www.blogger.com/blogger.g?blogID=5004180000000060001
I think you understand where the BlogID is…

3.Or…directly a function to post to blogspot, or two…

public void sendPostToBlogspot(string blogId, string username, string password, string title, string postBody, string tags)
{
    CheckForIllegalCrossThreadCalls = false;
    Service service = new Service("blogger", "googleAPIshittyName");
    service.Credentials = new GDataCredentials(username, password);
    GDataGAuthRequestFactory factory = (GDataGAuthRequestFactory)service.RequestFactory;
    factory.AccountType = "GOOGLE";
    /////////////////////////////////////////////////////////////

    if (postBody.Contains("[IMGLINK]"))
    {
		postBody = postBody.Replace("[IMGLINK]", uploadImageToGoogle("image.png", username, password, blogId));
    }

    ///////post////////////////////////////////
    AtomEntry post = new AtomEntry();
    post.Title.Text = title;
    post.Content = new AtomContent();
    post.Content.Content = postBody;
    post.Content.Type = "xhtml";
    int length = 0;
    //make sure that tags are not too long
    string[] tagsS = tags.Split(',');
    foreach (string label in tagsS)
    {
		length = length + label.Count();
		if (length > 198)
		{
			string label2 = label.Substring(0, 199);
			AtomCategory cat = new AtomCategory();
			cat.Scheme = new Uri("http://www.blogger.com/atom/ns#");
			cat.Term = label2;
			post.Categories.Add(cat);
			this.maskedTextBox5.Text = this.maskedTextBox5.Text + label2 + ",";
			break;
		}
		else
		{
			AtomCategory cat = new AtomCategory();
			cat.Scheme = new Uri("http://www.blogger.com/atom/ns#");
			cat.Term = label;
			post.Categories.Add(cat);
		}
    }
    post.IsDraft = false;

    ///////////////publishing post/_/_/_/_/_/_\_|_/_)_(_--> () - {} - [] 0_o
    Uri blogFeedUri = new Uri("http://www.blogger.com/feeds/" + blogId + "/posts/default");
    AtomEntry createdEntry = null;
    try
    {
		createdEntry = service.Insert(blogFeedUri, post);
		MessageBox.Show("Done!");
    }
    catch (GDataRequestException exception)
    {
		if (exception.ResponseString == "Blog has exceeded rate limit or otherwise requires word verification for new posts")
			MessageBox.Show("Blog has exceeded rate limit or otherwise requires word verification for new posts");
    }
    if (createdEntry == null)
		MessageBox.Show("Something went wrong, atricle was not published...\nCheck all html tags...");
    }

public string uploadImageToGoogle(string path, string username, string password, string blogId)
{
    if (!System.IO.File.Exists(path))
        return "Error! Image file not found";
    ///////////////////////token session and shits...///////////
    PicasaService service = new PicasaService("HowToFixPRO");
    service.setUserCredentials(username, password);
    /////////////////cdefault album is dropBox or something////
    Uri postUri = new Uri(PicasaQuery.CreatePicasaUri(username));

    System.IO.FileInfo fileInfo = new System.IO.FileInfo(path);
    System.IO.FileStream fileStream = fileInfo.OpenRead();

    PicasaEntry entry = (PicasaEntry)service.Insert(postUri, fileStream, "image/png", "nameOfYourFile");
    fileStream.Close();

    PhotoAccessor ac = new PhotoAccessor(entry);
    string contentUrl = entry.Media.Content.Attributes["url"] as string;

    return contentUrl;
}

If you have images in your post you have to make sure that post will look like:
<img src="[IMGLINK]" /> vwhere [IMGLINK] will e replaced with image link which you have selected path for.

3.A little C# Application which will post to Blogspot:

google api post to blogspot net application

You can download C# Post to Blogspot using Google API project here.

You had to have libs on C:\Libs\ and this folder with libs C:\Libs\Google API\Samples

5.Conclusions

You can find more about Google API on Google GData Developers Forums like creating albums and others but I think these examples are a good point to start.

Please comment if worked for you or not and why not if yes :))

Related posts:

16 Comments on "Posting to Blogspot using Google GData API"

  1. Ly Hoang Dong says:

    i use this api to upload but how to upload more 50 articles per day?
    Thank!

    • admin says:

      I think the key is to post an article from blogger panel after you have reached the limit. Then wait 5 minutes and should work.

  2. Taner says:

    Hi i have a problem.

    The entity "nbsp " was referenced, but not declared. Error.

    how can i fix?

  3. Johne995 says:

    Wow! This could be one particular of the most useful blogs We’ve ever arrive across on this subject. Basically Wonderful. I’m also a specialist in this topic so I can understand your hard work.

  4. Frodo says:

    Big thank for this launch plate m8! 😉

  5. sizar says:

    hi can you please explain using example how to update blog post .

  6. nuri says:

    can you share this program’s project datas.

  7. LabLance says:

    how to set images and font style.

  8. Tam says:

    Error :
    “An unhandled exception of type ‘Google.GData.Client.InvalidCredentialsException’ occurred in Google.GData.Client.dll”
    Line:
    “createdEntry = service.Insert(blogFeedUri, post);”

    Can you help me?

  9. taneaonline says:

    Blog has exceeded rate limit or otherwise requires word verification for new posts

  10. taneaonline says:

    Blog has exceeded rate limit or otherwise requires word verification for new posts

    Please advise!

  11. XTY says:

    Hi, i used your program for a long time and recently it stopped working, gives exception… “Execution of authentication request returned unexpected result: 404”
    i found a site where they talk about it and some say they found the solution, but i don’t know how to implement it in the program, could the admin or someone else take a look and update the code?
    Thank you very much!!!!!!!!!!!!!!!

  12. XTY says:

    I forgot to write the website of the solutions:
    https://code.google.com/p/google-apps-script-issues/issues/detail?id=5108
    Thank you, have a good day!

  13. thirumoorthi Murugan says:

    Hi ,
    I am getting below error

    Execution of authentication request returned unexpected result: 404

    Can you please tell me how to fix this.

Got something to say? Go for it!