home

Post onto blogger / blogspot with Ruby programmatically

An example to post onto Google blogger / blogspot with ruby programmatically.

This is a straight forward example to show how you can make a post to google blogger (blogspot) with ruby. The full Google GData API for blogger can be found in Google Data Blogger API Deverloper’s guide

require ‘net/http’
require ‘net/https’

googleEmail = ‘XXXXXX@gmail.com’
googlepasswd = ‘XXXXXX
source = ‘ladydeals.com-rubyPost-1.0#unimportant, google recommend it in <companyName-applicationName-versionID> format
blogId = ‘12345678901234567890 # Go to blogspot dashboard and click on your blog’s “Posts” link and you will see the blog ID in the URL

http = Net::HTTP.new(’www.google.com’, 443)
http.use_ssl = true
urlPath = ‘/accounts/ClientLogin’

# Setup HTTPS request post data to obtain authentication token.

data = ‘Email=’ + googleEmail +’&Passwd=’ + googlepasswd + ‘&source=’ + source + ‘&service=blogger’# Setup HTTPS request header to obtain authentication token.
headers = {
‘Content-Type’ => ‘application/x-www-form-urlencoded’
}

# Submit HTTPS post request
resp, data = http.post(urlPath, data, headers)

# Output on the screen -> we should get either a 302 redirect (after a successful login) or an error page
# Expect resp.code == 200 and resp.message == ‘OK’ for a successful.


if (!(resp.code.eql? ‘200′))
puts ‘Code = ‘ + resp.code
puts ‘Message = ‘ + resp.message
else


resp.each {|key, val| puts key + ‘ = ‘ + val}
# The responsed data will contain three lines which hold SID, LSID, and Auth. We can disregard SID and LSID
puts data

# Parse for the authentication token.
dataLines = data.split(”\n”)
authPair = dataLines[2]
authPairArray = authPair.split(”=”)
authToken = authPairArray[1]

# Setup HTTP request post head to make a blog post.
headers = {
‘Authorization’ => ‘GoogleLogin auth=’ + authToken,
‘Content-Type’ => ‘application/atom+xml’
}

# Setup HTTP request data to make a blog post.
data = <<dataEnd
<entry xmlns=’http://www.w3.org/2005/Atom’>
<title type=’text’>Hello World!</title>
<content type=’xhtml’>
<div xmlns=”http://www.w3.org/1999/xhtml”>
<p>I would like to say hello!</p>
</div>

</content>
</entry>
dataEnd

http = Net::HTTP.new(’www.blogger.com’)
path = ‘/feeds/’ + blogId + ‘/posts/default’

resp, data = http.post(path, data, headers)

# Expect resp.code == 200 and resp.message == ‘OK’ for a successful.

end