Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
641 views
in Technique[技术] by (71.8m points)

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Required reading:

Issue:

  • Usage of asynchronous client side browser samples in the synchronous server side.

Solution:

  • It is possible to access Blogger api from Google apps script using UrlFetchApp
  • Full OAuth flow can be bypassed using oauth token provided by ScriptApp
  • Include scopes in the appsscript.json manifest file.
  • Switch to a standard GCP and enable the blogger api

Snippet:

function createBlogPost(){
  var postUrl = "https://www.googleapis.com/blogger/v3/blogs/blogId/posts";
  var blogId = /*"YOUR_BLOG_ID"*/;
  postUrl = postUrl.replace("blogId",blogId);
  var options = {
    method:"post",
    contentType:"application/json",
    headers: { Authorization: "Bearer "+ ScriptApp.getOAuthToken()},
    muteHttpExceptions: true,
    payload: JSON.stringify({
      title: "Hello from Apps Script!",
      content: "This post is automatically created by Apps script"
    })
  }
  var res = UrlFetchApp.fetch(postUrl, options).getContentText();
  console.log(res);//or Logger.log(res)
}

Manifest scopes:

"oauthScopes": [
  "https://www.googleapis.com/auth/blogger",
  "https://www.googleapis.com/auth/script.external_request"
]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...