Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Creating a page from Jira using Authentication

Gleb Kartashov
Contributor
May 19, 2021

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?

2 answers

0 votes
PD Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 20, 2021

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
)
0 votes
Hana Kučerová
Community Champion
May 20, 2021

Hi @Gleb Kartashov ,

have you tried to use

def authenticatedRequestFactory = confluenceLink.createImpersonatingAuthenticatedRequestFactory()

?

See here for more info.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events