融合:使用VBA更新现有页面

时间:2018-08-07 13:52:55

标签: vba excel-vba confluence confluence-rest-api

我尝试使用VBA更新Confluence页面。 我的想法是使用REST API加载页面的内容,修改内容,然后上传修改后的版本。

这是我的代码

Private Sub TestRESTApi()
Dim uname As String
uname = "XXXX" '
Dim pw As String
pw = "XXXX"


Set requester = CreateObject("WinHttp.WinHttpRequest.5.1")
Dim ret As String
Dim send_str As String
With requester
    .Open "GET", "https://XXXX/rest/api/content/20057665?expand=body.storage", False
    .setRequestHeader "Content-Type", "application/json"
    .setRequestHeader "Accept", "application/json"
    .setRequestHeader "Authorization", "Basic"
    .SetCredentials uname, pw, 0
    .Send
    ret = .responseText
    Debug.Print (ret)
End With

'Do replacement stuff here
'send_str = ret with replacements in the "value" part


Set sender = CreateObject("WinHttp.WinHttpRequest.5.1")
With sender
    .Open "PUT", "https://XXXX/rest/api/content/20057665", False
    .setRequestHeader "Content-Type", "application/json"
    .setRequestHeader "Authorization", "Basic"
    .SetCredentials uname, pw, 0
    .Send send_str
    Debug.Print (.responseText)

End With
End Sub

GET请求有效,我返回一个json:

{
    "_expandable": {
        "ancestors": "",
        "children": "/rest/api/content/20057665/child",
        "container": "/rest/api/space/XXXX",
        "descendants": "/rest/api/content/20057665/descendant",
        "history": "/rest/api/content/20057665/history",
        "metadata": "",
        "operations": "",
        "restrictions": "/rest/api/content/20057665/restriction/byOperation",
        "space": "/rest/api/space/XXXX",
        "version": ""
    },
    "_links": {
        "base": "https://XXXX",
        "collection": "/rest/api/content",
        "context": "",
        "edit": "/pages/resumedraft.action?draftId=20057665&draftShareId=9c5eaf5c-d9c9-44d6-b7b6-317714f7581f",
        "self": "https://XXXX/rest/api/content/20057665",
        "tinyui": "XXXX",
        "webui": "/display/XXXX/Sandbox"
    },
    "body": {
        "_expandable": {
            "anonymous_export_view": "",
            "editor": "",
            "export_view": "",
            "styled_view": "",
            "view": ""
        },
        "storage": {
            "_expandable": {
                "content": "/rest/api/content/20057665"
            },
            "representation": "storage",
            "value": "THE CONTENT OF THE REQUESTET PAGE AS HTML"
        }
    },
    "extensions": {
        "position": "none"
    },
    "id": "20057665",
    "status": "current",
    "title": "Sandbox",
    "type": "page"
}

但是当我尝试使用修改后的内容更新页面时,出现以下错误消息:

{   
      "statusCode":400,   
      "data":   {
          "authorized":false,
          "valid":true,
          "errors":[],
          "successful":false   
      },   
      "message":"Must supply an incremented version when updating Content. No version supplied." 
}

但是GET响应中没有页面的当前版本。 我从哪里获得当前版本号,我必须在哪里写入版本号?

1 个答案:

答案 0 :(得分:1)

我自己找到了解决方案。 当我请求页面的当前版本时,我必须在请求中添加,version。 ({.Open "GET", "https://XXXX/rest/api/content/20057665?expand=body.storage,version", False而不是.Open "GET", "https://XXXX/rest/api/content/20057665?expand=body.storage", False)。因此,我会在响应中获取有关当前版本的信息:

   "version":{  
      "by":{  
         "type":"known",
         "username":"XXXX",
         "userKey":"XXXX",
         "profilePicture":{  
            "path":"/images/icons/profilepics/default.png",
            "width":48,
            "height":48,
            "isDefault":true
         },
         "displayName":"XXXX",
         "_links":{  
            "self":"https://XXXX/rest/experimental/user?key=XXXX"
         },
         "_expandable":{  
            "status":""
         }
      },
      "when":"2018-08-07T08:31:41.225+02:00",
      "message":"",
      "number":2,
      "minorEdit":false,
      "hidden":false,
      "_links":{  
         "self":"https://XXXX/rest/experimental/content/20057665/version/2"
      }

在这里,我可以增加数字,并且融合服务器接受我的更新。