Saturday, 8 March 2014

[SCRIPT] How to perform bulk posting to Blogger via Google Blogger API without Client and

// var gapi = require('googleapis'); // Cannot use it
var https = require('https');
// Template for Post content
swig = require('swig'),
extras = require('swig-extras');
extras.useFilter(swig, 'truncate');
var template = swig.compileFile('ABSOLUTE-PATH-TO-TEMPLATE/template.html');
// Read file containing information
var posts = require('./posts.json');
// Access google api documentation at this page: https://developers.google.com/blogger/docs/3.0/reference/posts/insert#try-it
// Login using OAuth and try an insert
// Copy the Authorization value in the Headers into our HEADER variable
var BLOG_ID = 'YOUR-BLOG-ID';
var METHOD = 'POST';
var HEADERS = { 'Content-Type': 'application/json', 'Authorization': 'YOUR-BEARER-KEY', 'X-JavaScript-User-Agent': 'Google APIs Explorer' };
var HOST = 'www.googleapis.com';
// YOUR_API_KEY must not be changed!
var ENDPOINT = '/blogger/v3/blogs/' + BLOG_ID + '/posts?fetchBody=false&fetchImages=false&isDraft=false&key={YOUR_API_KEY}'
var googleApiRequestForBloggerOptions = {
host: HOST,
path: ENDPOINT,
method: METHOD,
headers: HEADERS
};
function googleApiRequestForBloggerRequest(googleApiPayload, success) {
var googleApiRequestForBlogger = https.request(googleApiRequestForBloggerOptions, function(res) {
res.setEncoding('utf-8');
var responseString = '';
res.on('data', function(data) {
responseString += data;
});
res.on('end', function() {
var responseObject = JSON.parse(responseString);
success(responseObject);
});
});
googleApiRequestForBlogger.write(googleApiPayload);
googleApiRequestForBlogger.end();
}
/*
POST https://www.googleapis.com/blogger/v3/blogs/------------------/posts?fetchBody=false&fetchImages=false&isDraft=false&key={YOUR_API_KEY}
Content-Type: application/json
Authorization: Bearer *****************************************************
X-JavaScript-User-Agent: Google APIs Explorer
{
"labels": [
"asd"
],
"title": "asd",
"content": "asd"
}
*/
// For each post
function postToBlogger()
{
var post = posts.shift();
// Render post content
var insertPost = { labels: post.tags, title: post.title, content: template(post) };
var payload = JSON.stringify(insertPost);
// Post it
googleApiRequestForBloggerRequest(payload, function(success) { if(success.kind) { console.log(success); } else { console.log('Will retry...'); console.log(success); posts.push(post); } } );
}
setInterval(postToBlogger, 2000);
[
{
"links" : ["http://www.serfdom.io/"],
"title" : "Service discovery and orchestration for Go",
"tags" : [ "golang", "software architecture" ]
},
{
"links" : ["http://longomatch.org/"],
"title" : "Free application to record, tag and analyse videos in real-time (targeted for sport/games)",
"tags" : [ "software", "video", "analysis" ]
},
{
"links" : ["http://beta.slashdot.org/story/199013"],
"title" : "Valve Prepping Source 2 Engine For VR",
"tags" : [ "vr", "valve", "game engine" ]
},
{
"links" : ["http://www.polygon.com/2014/3/5/5458072/portal-board-game-interview-valve-cryptozoic-entertainment"],
"title" : "Portal Board Game by Cryptozoic",
"tags" : [ "board game", "valve", "portal" ]
},
{
"links" : ["http://www.engadget.com/2014/03/04/apple-carplay-ferrari-ff-hands-on/?ncid=rss_truncated", "http://www.theregister.co.uk/2014/03/02/mercedes_plots_fandroid_future/", "http://www.engadget.com/2014/03/02/google-projected-mode-mercedes-benz-leak/?ncid=rss_truncated"],
"title" : "Google and Automotive IVI, Apple and CarPlay on Ferrari FF. Who will win?",
"tags" : [ "google", "apple", "automotive", "ivi", "carplay", "ferrari" ]
},
{
"links" : ["http://www.engadget.com/2014/03/04/skype-for-outlook-com-is-now-available-worldwide/?ncid=rss_truncated"],
"title" : "Skype on the browser. Finally.",
"tags" : [ "skype", "webapp" ]
},
{
"links" : ["http://www.mashery.com/product/io-docs", "http://swagger.wordnik.com/", "http://apiblueprint.org/", "http://raml.org/index.html"],
"title" : "Different ways to document/generate or model your REST API",
"tags" : [ "rest api", "documentation" ]
},
{
"links" : ["http://linuxgizmos.com/qt-embedded-gui-adds-yocto-recipes-hops-up-emulator/", "https://www.yoctoproject.org/"],
"title" : "Qt Embedded gains new platforms. Yocto link to provide some context.",
"tags" : [ "qt", "embedded", "yocto" ]
}
]
view raw posts.json hosted with ❤ by GitHub
<p>
{% if desc %}{{ desc }}<br/>{% endif %}
<ul>
{% for link in links %}
<li><a href="{{ link }}">{{ link|truncate(60) }}</a></li>
{% endfor %}
</ul>
</p>
view raw template.html hosted with ❤ by GitHub
Note that you should get your Blogger API Key at Google API Console.
Blogger API is experimental AND must be requested explicitly.

But getting the Auth key from the Headers of the Google Blogger Developers Documentation and performing the OAuth login, you'll be able to perform calls to the REST API.

No comments: