0

hi all iam trying insert post using GAS but failed.. can you tell me what im wrong... thx in advance....

here my code

`function sendHttpPost() {
 var API_KEY = 'my api key';
 var scope = "http://www.blogger.com/feeds/";       
 var oAuthConfig = UrlFetchApp.addOAuthService("blogger");
 oAuthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope="+scope);
 oAuthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken");
 oAuthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
 oAuthConfig.setConsumerKey("anonymous");
 oAuthConfig.setConsumerSecret("anonymous");
  var payload = 
    {
  "kind": "blogger#post",
  "blog": {
    "id": "486683248036684073"
  },
  "title": "A new post",
  "content": "With <b>exciting</b> content..."
}
  var options =
    {
      "contentType":"application/json",
      "oAuthServiceName" : "blogger",
      "oAuthUseToken" : "always",
      "method" : "POST",
      "payload" : payload
    };

  var respon = UrlFetchApp.fetch("https://www.googleapis.com/blogger/v3/blogs/486683248036684073/posts?key="+API_KEY, options);

and here is error message

Request failed for returned code 400. Server response: { "error": { "errors": [ { "domain": "global", "reason": "parseError", "message": "Parse Error" } ], "code": 400, "message": "Parse Error" } }

Meta Morfoself
  • 103
  • 1
  • 2
  • 9
  • The error is pretty vague but I'd start with replacing the content with some simple text of say one line. – Srik Mar 14 '13 at 23:55

2 Answers2

0

I believe you are trying to use oauth1 when oauth2 is required. there already is a unanswered request about that here. Implementing oauth 2 with Google app script is really a pain, so I made an attempt to build a library that could answer the need (dioxygen library) - it work a little bit like the oauth2 playground but it's less pretty. With a little work you should be able to adapt it to your need with blogger.

Community
  • 1
  • 1
Harold
  • 3,297
  • 1
  • 18
  • 26
0

I tried Harold's library, but after successfully retrieving OAuth token, I ended up with the same error.

But, when I issued the same JSON request as in my script through the API Explorer, it was processed: https://developers.google.com/blogger/docs/3.0/reference/posts/insert

[UPDATE]

I am taking it back. This code works. I just replaced the payload variable and put the JSON request straight into URL fetch options. So there was some problem with passing that payload variable into options variable.

function testBlogger() {

  var payload =
      {
        "kind": "blogger#post",
        "blog": {
          "id": "YOUR_BLOG_ID"
        },
        "title": "New post",
        "content": "With content..."
      };

  var options =
      {
        "method" : "post",
        "headers" : { "Authorization" : "Bearer YOUR_ACTIVE_TOKEN"},
        "contentType" : "application/json",
        "payload" : '{ "kind": "blogger#post", "blog": { "id": "YOUR_BLOG_ID" }, "title": "New post", "content": "With content..." }'
      };

  try {

    var result = UrlFetchApp.fetch(
      "https://www.googleapis.com/blogger/v3/blogs/YOUR_BLOG_ID/posts",
      options);

    Logger.log(result);

  } catch (e) {
    Logger.log(e);
  }
}
jogo
  • 91
  • 1
  • 2