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

dev*_*ent 1 api ajax rest jquery confluence

我一直在寻找有关如何使用其REST API在Confluence中创建页面的纯HTML和jQuery示例。我找到了以下示例,但它们都不对我有用:

例子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);
    }
});
Run Code Online (Sandbox Code Playgroud)

例子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!');
    }
});
Run Code Online (Sandbox Code Playgroud)

我的密码

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);
      }
});
Run Code Online (Sandbox Code Playgroud)

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

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

我发现发布了类似的问题,但没有一个有可行的解决方案。大多数资源似乎都是为python编写的。

是否有另一个API解决方案会更好?我愿意提出建议。

dev*_*ent 5

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

beforeSend: function (xhr){
    xhr.setRequestHeader('Authorization', "Basic YWRtaW46YWRtaW4= " );
}
Run Code Online (Sandbox Code Playgroud)

要么

headers: {
    "Authorization": "Basic " + btoa(username + ":" + password)
}
Run Code Online (Sandbox Code Playgroud)

使用带有用户名和密码的xhrFields选项似乎可以执行成功的请求。

我的解决方案

$.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);
    }
});
Run Code Online (Sandbox Code Playgroud)