Hi
I'm using Groovy script to create Conflunce pages automatically using ApplicationLink methods:
def confluenceLink = getPrimaryConfluenceLink()
def authenticatedRequestFactory = confluenceLink.createAuthenticatedRequestFactory()
authenticatedRequestFactory
.createRequest(Request.MethodType.PUT, "rest/api/content/" + GetInsightValue(object.getId(), 1101) + "")
.addHeader("Content-Type", "application/json")
.setRequestBody(new JsonBuilder(params).toString())
.execute(new ResponseHandler<Response>() {
@Override
void handle(Response response) throws ResponseException {
if(response.statusCode != HttpURLConnection.HTTP_OK) {
throw new Exception(response.getResponseBodyAsString())
} else {
Object page = new JsonSlurper().parseText(response.getResponseBodyAsString())
String pageTitleq = page["id"]
setInsightPageValue(Integer.valueOf(pageTitleq))
log.error('Success!')
}
}
})
The documentation page doesn't mention any methods besides execute althogh I've found the other methods' usage online (addHeader, setRequestBody in my case) and used them in my code.
This script was working fine until my company updated security policies and now anonymous page creation/updating is forbidden and I have to add authentication to this script. It should be always one user ("robot") that creates/updates pages so no user input is required. I couldn't find any useful info about adding auth to the authenticatedRequestFactory type request but the existence of addHeader method and it's parameters that I used imply that I should be fine adding "default" REST auth header.
Question is will it work or will I have to rewrite the whole script using differet request method for Confluence?
If you will be using this "robot" user, presumably you know its password.
Then I would recommend you use the simpler RESTClient to make your calls.
import com.atlassian.applinks.api.application.confluence.ConfluenceApplicationType
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.applinks.api.ApplicationLinkService
import groovyx.net.http.RESTClient
import groovyx.net.http.ContentType
def linkService = ComponentLocator.getComponent(ApplicationLinkService)
def confBaseUrl = linkService.getApplicationLinks(ConfluenceApplicationType.class).first().rpcUrl
def restClient = new RESTClient(confBaseUrl)
restClient.auth.basic 'robotUn', 'robotPw'
restClient.put(
requestContentType: ContentType.JSON,
path: "rest/api/content/${GetInsightValue(object.getId(), 1101)}",
body: params //no need to build the json, this should do it for you
)
Hi @Gleb Kartashov ,
have you tried to use
def authenticatedRequestFactory = confluenceLink.createImpersonatingAuthenticatedRequestFactory()
?
See here for more info.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.