Google 用于创建 Confluence 页面的 Apps 脚本 -- 结果状态代码 500

Google Apps Script to Create Confluence Page -- Results in Status Code 500

注意:我还在此处的 Atlassian 论坛上发布了这个问题: https://community.atlassian.com/t5/Confluence-questions/Google-Apps-Script-to-Create-Confluence-Page-HTTP-500-Error/qaq-p/1039040#M66094

我在 SO 上接触到更多的观众。

我在 Google Sheet 中使用以下 google 应用程序脚本代码来创建 Confluence 页面:

headers = {"Authorization" : "Basic " + Utilities.base64Encode(user + ':' + 
pswd), "Content-Type": "application/json"};

url = "https://confluence.asdf.com/rest/api/content/";
var params = { 
  "method":"POST",
  "headers":headers,
  "muteHttpExceptions": false,
  "type":"page",
  "title":newTitle,
  "space":{"key":"DOC"},
  "body":{"storage":{"value": "<p>This is <br/> a new page</p>" }, 
  "representation":"storage"} 
};
var createResponse = UrlFetchApp.fetch(url, params); 

但是,当我提交请求时,我收到了这样的回复:

Request failed for https://confluence.atlas.asdf.com/rest/api/content/ 
returned code 500.
Truncated server response: {"statusCode":500,"
message":"<?> No content to map to Object due to end of 
input","reason":"Internal Server Error"} (use muteHttpExceptions option to 
examine full response)

我知道那里有我见过的卷曲样本,但对我没有帮助。 我做错了什么?

编辑:3 月 25 日 @田池 我按照你的建议修改了代码:

    headers = {"Authorization" : "Basic " + Utilities.base64Encode(user + ':' + pswd) };


    var payload = {
      "type": "page",
      "title": newTitle,
      "space": {"key": "DOC"},
      "body": {
        "storage": {
          "value": "<p>This is <br/> a new page</p>"
        },
        "representation": "storage"
      }
    };
    var params = { 
      "method": "POST",
      "headers": headers,
      "muteHttpExceptions": false,
      "payload": JSON.stringify(payload),
      "contentType": "application/json"
    };

    var createResponse = UrlFetchApp.fetch(url, params);        

我收到和以前一样的错误。

这个修改怎么样?

修改后的脚本:

请修改params如下,重新测试。从您问题中的错误消息来看,请求正文已放在 payload.

的 属性 中
var payload = {
  "type": "page",
  "title": newTitle,
  "space": {"key": "DOC"},
  "body": {
    "storage": {
      "value": "<p>This is <br/> a new page</p>"
    },
    "representation": "storage"
  }
};
var params = { 
  "method": "POST",
  "headers": headers,
  "muteHttpExceptions": false,
  "payload": JSON.stringify(payload)
};

注:

  • 此修改假设payloadheaders中的每个值都是正确的。
  • "Content-Type": "application/json" in headers 也可以放在 params as "contentType": "application/json".

参考文献:

我无法测试此修改。所以如果这不起作用,你能提供错误信息吗?我想考虑一下这个问题。

编辑:

the official document看来,body的属性好像是"body":{"storage":{"value":"<p>This is a new page</p>","representation":"storage"}}}。所以请修改如下。

修改后的脚本:

headers = {"Authorization" : "Basic " + Utilities.base64Encode(user + ':' + pswd) };
var payload = {
  "type": "page",
  "title": newTitle,
  "space": {"key": "DOC"},
  "body": {
    "storage": {
      "value": "<p>This is <br/> a new page</p>",
      "representation": "storage" // Modified
    }
  }
};
var params = { 
  "method": "POST",
  "headers": headers,
  "muteHttpExceptions": false,
  "payload": JSON.stringify(payload),
  "contentType": "application/json"
};
var createResponse = UrlFetchApp.fetch(url, params);

注:

  • 在我的环境中,我可以确认结果与官方示例脚本的结果相同。如果这不起作用,请再次确认 payloadheaders 的每个值。