Confluence:使用 VBA 更新现有页面
Confluence: Update an existing page using VBA
我尝试使用 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 响应中没有页面的当前版本。
我从哪里获取当前版本号以及我必须将版本号写入哪里?
我自己找到了解决方案。
当我请求页面的当前版本时,我必须将 ,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"
}
在这里我可以递增数字并且汇合服务器接受我的更新。
我尝试使用 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 响应中没有页面的当前版本。 我从哪里获取当前版本号以及我必须将版本号写入哪里?
我自己找到了解决方案。
当我请求页面的当前版本时,我必须将 ,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"
}
在这里我可以递增数字并且汇合服务器接受我的更新。