Create a post in Blogger with Google Apps Script

So far I haven’t found a good code to create posts in Blogger with Google Script.

In the API Console I got the following credentials:

  • Client ID
  • Client Secret
  • API key

Also, libraries were added to the Google Script:

  • OAuth2 library → MswhXl8fVhTFUH_Q3UOJbXvxhMjh3Sh48
  • Blogger library → M2CuWgtxF1cPLI9mdRG5_9sh00DPSBbB3

I tried some codes, and this is the current one:

function create_blog_post() {
  var payload =
      {
        "kind": "blogger#post",
        "blog": {
          "id": "12345........" // YOUR_BLOG_ID
        },
        "title": "New post",
        "content": "With content..."
      };
var headers = {
    "Authorization": "Bearer " + getService().getAccessToken(), // ← THIS IS WRONG
    "X-HTTP-Method-Override": "PATCH"
  };
  var options =
      {
        "method" : "post",
        "headers" : { "Authorization" : "Bearer" + getService().getAccessToken()},
        "contentType" : "application/json",
        "payload" : '{ "kind": "blogger#post", "blog": { "id": "12345........" }, "title": "New post", "content": "With content..." }'
      };
  try {
    var result = UrlFetchApp.fetch(
      "https://www.googleapis.com/blogger/v3/blogs/12345......../posts", options);
    Logger.log(result);
    } catch (e) {Logger.log(e);}
}

Please help me solve this with the simplest code possible.