如何使用 jQuery 及其 REST API 在 Confluence 中创建新页面?

How to create a new page in Confluence using jQuery and their REST API?

我一直在寻找一个纯粹的 HTML 和 jQuery 示例来说明如何使用他们的 REST API 在 Confluence 中创建页面。我找到了以下示例,但它们都不适合我:

示例 A

var username = "admin";
var password = "admin";
var jsondata = {"type":"page",
"title":"My Test Page",
"space":{"key":"TST"},
"body":{"storage":{"value":"<p>This is a new page</p>","representation":"storage"}}};

$.ajax
  ({
    type: "POST",
    url: "http://localhost:8080/confluence/rest/api/content/",
    contentType:"application/json; charset=utf-8",
    dataType: "json",
    async: false,
    headers: {
        "Authorization": "Basic " + btoa(username+ ":" + password)
    },
    data: JSON.stringify(jsondata),
    success: function (){
        console.log('Page saved!');
    },
    error : function(xhr, errorText){
        console.log('Error '+ xhr.responseText);
    }
});

示例 B

var jsondata = {"type":"page",
"title":"My Test Page",
"space":{"key":"TEST"},
"body":{"storage":{"value":"<p>This is a new page</p>","representation":"storage"}}}

$.ajax
  ({
    type: "POST",
    url: "http://localhost:8080/confluence/rest/api/content/",
    contentType:"application/json; charset=utf-8",
    dataType: "json",
    async: false,
    beforeSend: function (xhr){
        xhr.setRequestHeader('Authorization', "Basic YWRtaW46YWRtaW4= " );
    },
    data: JSON.stringify(jsondata),
    success: function (){
        console.log('Page saved!');
    }
});

我的代码

var data = {
      type: "page",
      title: "New Page",
      space: {
            key: "TST"
      },
      body: {
            storage: {
                  value: "<p>This is a new page</p>",
                  representation: "storage"
            }
      }
};

var link = 'http://confluence.mysite.com:80/rest/api/content/';

$.ajax({
      url: link,
      type: "POST",
      dataType: "json",
      crossDomain: true,
      username: user.username,
      password: user.password,
      data: JSON.stringify(data),
      contentType: "application/json",
      success: function (result) {
            alert('Data saved!');
      },
      error: function (xhr, status, error) {
            alert('Status: ' + status + '\n' +
                   'Error: ' + error);
      }
});

我尝试了以下解决方案(以上),但我似乎无法针对 Confluence 进行身份验证。我检查了权限,我是 Confluence 管理组的成员。 GET 命令似乎有效,但是使用 POST,我不断得到:

({"statusCode":403,"message":"You're not allowed to view that space, or it does not exist."})

我发现了类似的问题,但其中 none 有有效的解决方案。大多数资源似乎是为 python.

编写的

是否有其他 API 解决方案被证明会更好?我乐于接受建议。

再次感谢@mansilladev。在您的帮助下,我找到了解决方案。而不是使用以下方法:

beforeSend: function (xhr){
    xhr.setRequestHeader('Authorization', "Basic YWRtaW46YWRtaW4= " );
}

headers: {
    "Authorization": "Basic " + btoa(username + ":" + password)
}

使用 xhrFields 选项,使用 username & password,似乎执行成功请求。

我的解决方案

$.ajax({
    url: link,
    type: "POST",
    dataType: "json",
    crossDomain: true,
    username: user.username,
    password: user.password,
    data: JSON.stringify(data),
    contentType: "application/json",
    xhrFields: {
            "withCredentials": true
    },
    success: function (result) {
            alert('Data saved!');
    },
    error: function (xhr, status, error) {
            alert(status + ' | ' + error);
    }
});