From 1c16921f3f52b6717212325daf1a4556690675c0 Mon Sep 17 00:00:00 2001 From: wiibaa Date: Sun, 19 Jan 2014 14:31:36 +0100 Subject: [PATCH] Allow publish to Blogger Pages --- public/res/helpers/googleHelper.js | 90 +++++++++++++++++++++ public/res/html/bodyIndex.html | 23 +++++- public/res/providers/bloggerPageProvider.js | 42 ++++++++++ public/res/publisher.js | 1 + public/res/styles/base.less | 2 +- 5 files changed, 156 insertions(+), 2 deletions(-) create mode 100644 public/res/providers/bloggerPageProvider.js diff --git a/public/res/helpers/googleHelper.js b/public/res/helpers/googleHelper.js index ce8cdb98..eae8f20e 100644 --- a/public/res/helpers/googleHelper.js +++ b/public/res/helpers/googleHelper.js @@ -942,6 +942,96 @@ define([ }); task.enqueue(); }; + + googleHelper.uploadBloggerPage = function(blogUrl, blogId, pageId, isDraft, publishDate, title, content, callback) { + var accountId = 'google.blogger0'; + var task = new AsyncTask(); + connect(task); + authenticate(task, 'blogger', accountId); + task.onRun(function() { + var headers = {}; + var authorizationMgr = authorizationMgrMap[accountId]; + if(authorizationMgr && authorizationMgr.token) { + headers.Authorization = "Bearer " + authorizationMgr.token.access_token; + } + function uploadPage() { + var url = "https://www.googleapis.com/blogger/v3/blogs/" + blogId + "/pages/"; + var data = { + kind: "blogger#page", + blog: { + id: blogId + }, + title: title, + content: content + }; + var type = "POST"; + // If it's an update + if(pageId !== undefined) { + url += pageId; + data.id = pageId; + type = "PUT"; + } + $.ajax({ + url: url, + data: JSON.stringify(data), + headers: headers, + type: type, + contentType: "application/json", + dataType: "json", + timeout: constants.AJAX_TIMEOUT + }).done(function(page) { + pageId = page.id; + task.chain(); + }).fail(function(jqXHR) { + var error = { + code: jqXHR.status, + message: jqXHR.statusText + }; + // Handle error + if(error.code === 404 && pageId !== undefined) { + error = 'Page ' + pageId + ' not found on Blogger.|removePublish'; + } + handleError(error, task); + }); + } + function getBlogId() { + if(blogId !== undefined) { + task.chain(uploadPage); + return; + } + $.ajax({ + url: "https://www.googleapis.com/blogger/v3/blogs/byurl", + data: { + url: blogUrl + }, + headers: headers, + dataType: "json", + timeout: constants.AJAX_TIMEOUT + }).done(function(blog) { + blogId = blog.id; + task.chain(uploadPage); + }).fail(function(jqXHR) { + var error = { + code: jqXHR.status, + message: jqXHR.statusText + }; + // Handle error + if(error.code === 404) { + error = 'Blog "' + blogUrl + '" not found on Blogger.|removePublish'; + } + handleError(error, task); + }); + } + task.chain(getBlogId); + }); + task.onSuccess(function() { + callback(undefined, blogId, pageId); + }); + task.onError(function(error) { + callback(error); + }); + task.enqueue(); + }; // Use by Google's client.js window.delayedFunction = undefined; diff --git a/public/res/html/bodyIndex.html b/public/res/html/bodyIndex.html index ba8c960b..50e04540 100644 --- a/public/res/html/bodyIndex.html +++ b/public/res/html/bodyIndex.html @@ -683,7 +683,7 @@ -