Search This Blog

Sunday, July 21, 2013

Post on Blogspot using cURL with bash

The following bash-script posts an article in the blogspot.

#!/bin/sh

# Authenticate
# Requires $GDATA_PASSWORD to be set as an environment variable.
G_AUTH_TOKEN=`curl 2>/dev/null https://www.google.com/accounts/ClientLogin \
    -d Email=YOUR_EMAIL_ADDRESS@example.com \
    -d Passwd=$GDATA_PASSWORD  \
    -d accountType=GOOGLE \
    -d source=curlExample \
    -d service=blogger \
  | grep '^Auth=' | cut -c 6-`

# Post on my blog.
curl -v --request POST -H "Content-Type: application/atom+xml" \
    -H "Authorization: GoogleLogin auth=$G_AUTH_TOKEN" \
    "http://www.blogger.com/feeds/YOUR_BLOGS_ID/posts/default" --data "@blog_post.xml"

The followings are the field specific for a user account and a blog.

YOUR_EMAIL_ADDRESS@example.com: user account for a blog
$GDATA_PASSWORD: user password
YOUR_BLOGS_ID: blog id
blog_post.xml: data for upload

The following is an xml example to upload.

<entry xmlns="http://www.w3.org/2005/Atom">  
  <title type="text">Knock knock</title>  
  <content type="xhtml">  
    <div xmlns="http://www.w3.org/1999/xhtml">  
      <p>Who's there?</p>  
      <p>Orange.</p>  
      <p>Orange who?</p>  
      <p>Orange you glad you know how to post using curl?!? (lol!)</p>  
    </div>  
  </content>  
  <category scheme="http://www.blogger.com/atom/ns#" term="joke"/>  
  <category scheme="http://www.blogger.com/atom/ns#" term="curl"/>  
</entry>  

In this xml, category scheme defines labels.

http://gdatatips.blogspot.com/2008/08/post-on-blogger-using-curl.html

Code formatter: http://formatmysourcecode.blogspot.com/2006/02/paste-your-text-here.html

No comments:

Post a Comment

Blog Archive