Using Blogger API with Google Apps Script

U
Published in: Google Apps Script - Blogger

Like WordPress, you can also manage your Blogger blogs using Google Apps Scripts. You need to enable the Blogger API from your Google Developers console and also include the Apps Script oAuth2 library in your Google Apps script project. The scope should be set to either of the following depending on whether to want read or write access to blogs.

https://www.googleapis.com/auth/blogger
https://www.googleapis.com/auth/blogger.readonly

The snippet connects to the Blogger API and fetches the list of Blogger blogs of the currently authenticated users. It then outputs the blog’s ID, name and blog URL in the console log.

function bloggerAPI() {

  var api = "https://www.googleapis.com/blogger/v3/users/self/blogs";

  var headers = {
    "Authorization": "Bearer " + getService().getAccessToken()
  };

  var options = {
    "headers": headers,
    "method" : "GET",
    "muteHttpExceptions": true
  };

  var response = UrlFetchApp.fetch(api, options);

  var json = JSON.parse(response.getContentText());

  for (var i in json.items) {
    Logger.log("[%s] %s %s", json.items[i].id, json.items[i].name, json.items[i].url);
  }
}

In the next example, we using the Blogger API to update the title and content of a blog post through Script. We update the post through Patch Semantics which allows us to send only fields that have changed or need to updated. Since UrlFetchApp doesn’t allow HTTP PATCH requests, we do an HTTP POST request and set the override X-HTTP-Method-Override header to PATCH, as shown below:

function updatePost(blogID, postID) {

  var url = "https://www.googleapis.com/blogger/v3/blogs/" + blogID + "/posts/" + postID;

  var payload = {
    "title"   : "This is the post title",
    "content" : "This is **HTML** post"
  };

  var headers = {
    "Authorization": "Bearer " + getService().getAccessToken(),
    "X-HTTP-Method-Override": "PATCH"
  };

  var options = {
    "headers": headers,
    "method" : "POST",
    "muteHttpExceptions" : true,
    "payload": JSON.stringify(payload),
    "contentType": "application/json"
  }

  var response = UrlFetchApp.fetch(url, options);

  Logger.log(response.getContentText());

}

Troubleshooting: If you fetching the post status (draft, live or scheduled), you need to set the view parameter as “ADMIN” in the API call.

For 403 forbidden errors that say “We’re sorry, but you don’t have permission to access this resource” - it is likely that you have only read-only or view access to a blog.

📮  Subscribe to our Email Newsletter for Google tips and tutorials!
Published in: Google Apps Script - Blogger

Looking for something? Find here!

Meet the Author

Web Geek, Google Developer Expert
A
Amit Agarwal

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India. He is the developer of Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

Get in touch

Google Add-ons

Do more with your Gmail and GSuite account
G

We build bespoke solutions that use the capabilities and the features of Google Workspace for automating business processes and driving work productivity.

  1. Mail Merge with Attachments
    Send personalized email to your Google Contact with a Google Sheet and Gmail
  2. Save Emails and Attachments
    Download email messages and file attachments from Gmail to your Google Drive
  3. Google Forms Email Notifications
    Send email notifications to multiple people when a new Google Form is submitted
  4. Document Studio
    Create beautiful pixel perfect documents merging data from Google Sheets and Google Forms
  5. Creator Studio for Google Slides
    Turn your Google Slides presentations into animated GIFs and videos for uploading to YouTube