Pages

[NodeJS] Pushing posts to Blogger from commandline

I was trying to write some small cli to push my posts to Blogger from the commandline. There are some tools out there, including googlecl and blogger-cli but no nodejs tool as far as I know. So, I registered to use Blogger API. Unfortunately, you'll have to wait 5 days just to get approval and instruction from Google, and that process is handled by only one man, Brett. So, while waiting for the API, here's my work around.

  1. From Google's code console, get your API key and stored it in ~/.bash_profile. Let's call it GOOGLE_API_KEY
  2. Head over to Blogger API sandbox and try some service that require authorization, e.g. inserting post. Note that you won't need this if you just want to get posts or comments or some actions that don't require authorization.
  3. Execute the service with the browser's console open. Watch in the Network tab for the post query
  4. In the query URL, look for key Bearer, then copy its value and store in ~/.bash_profile. Let's call it BLOGGER_TOKEN
  5. Also, get your blog's ID and save it in ~/.bash_profile as well. Let's call it BLOG_ID

Following is the simplified code snippet allows you to push your content to Blogger.


let nconf = require('nconf'),
    request = require('request');

nconf.argv()
     .env();

let apiKey = nconf.get('GOOGLE_API_KEY'),
    bToken = nconf.get('BLOGGER_TOKEN'),
    bID = nconf.get('BLOG_ID'),
    bURI = 'https://www.googleapis.com/blogger/v3/blogs/' + bID;

request.post(
  {
    url: bURI + '/posts?' + 
                'fetchBody=true' + 
                '&isDraft=true' + 
                '&key=' + apiKey,
    form: {
      labels: ['nodejs', 'sandbox'],
      title: 'Test CLI',
      content: 'This post was pushed from command line.'
    },
    auth: { bearer: bToken }
  },
  function(error, response) {
    if (!error) {
      console.log('Mission completed!', response.statusCode);
    }
    else {
      throw error;
    }  
  }
);

NOTE: the main problem with this approach is the bearer token expires after certain period of time

16 comments:

  1. looking for remote developer jobs? Take a look at http://webwork.io

    ReplyDelete
  2. Excellent post--I'm glad I found it.Thanks for sharing...
    node.js development services

    ReplyDelete
  3. Thank you.Well it was nice post and very helpful information on AngularJS Online Training

    ReplyDelete
  4. khong biet lay BLOGGER_TOKEN nhu the nao admin oi?

    ReplyDelete
  5. Enjoyed reading the article above, really explains everything in detail, the article is very interesting and effective. Thank you and good luck for the upcoming articles learn Node JS training

    ReplyDelete
  6. Hey!!Nice post i got to learn so many things.
    Please Check out this one Node JS

    ReplyDelete
  7. nice post!!!!
    Learn more about this check here Nodejs

    ReplyDelete
  8. Rails is basically a web application framework, which is consist of everything needs to create database baked web application. It helps the developers to create websites and applications by providing structures for all codes written by them. Moreover, common repetitive tasks are simplified with the help of this technology.
    ruby on rails software house
    Popular rails gems and APIs
    Websites made with ruby
    Best ruby gems 2019
    React native and React Js
    Node Js and React Js

    ReplyDelete
  9. One software for all your problems- Nodejs web development framework. We believe in delivering the best and that’s why Inwizards nodejs development company is in the software industry for more than a decade.

    Why hire node js developers from Inwizards?
    Highly experienced Nodejs Developers
    Mastery of latest technologies
    Transparent workflow
    Affordable prices
    500+ successful projects
    24*7 Support
    Custom Application Development
    Rich client history

    If you want to achieve success for your business, Inwizards LLC provides the best node js development services.

    Feel free to get in touch with us for further discussions and pricing details. Here are our contact details:

    Website Address: https://inwizards.com/nodejs-development-company.php

    Email: info@inwizards.com

    Call: USA - +1- (979)-599-0896 | IN- +91-9667584436

    ReplyDelete
  10. We’ll take care of your IT goals so let’s build something great together. Inwizards NodeJS development company is your compatible partner. Build nodejs web development applications with ease at Inwizards!

    Why hire NodeJS developers?

    Highly experienced Nodejs Developers
    Mastery of latest technologies
    Transparent workflow
    Affordable prices
    500+ successful projects
    24*7 Support
    Custom Application Development
    Rich client history

    If you want to achieve success for your business, Inwizards LLC provides the best NodeJS development services.

    Feel free to get in touch with us for further discussions and pricing details. Here are our contact details:

    Website Address: https://www.inwizards.com/nodejs-development-company.php

    Email: info@inwizards.com

    Call: USA - +1-(979)-599-0896 | IN- +91-9667584436

    ReplyDelete
  11. Thanks for sharing nice information. Find the best nodejs development services for your business.

    ReplyDelete
  12. Good Informative Blogs! For Projects like Ecommerce, Delivery Apps Node JS is Prefered! If you have any questions on Node JS Development you can talk to Echoinnovate IT!

    ReplyDelete
  13. We appreciate your blog article very much. There are many of alternative options after visiting your post. I knew exactly what I was searching for. I value your posts and I hope you keep them up. Fantastic work.

    Best Software Development Company In Mohali

    Mobile App Development Company

    ReplyDelete