Creating a page from Jira using Authentication
Asked by Gleb Kartashov:

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](https://docs.atlassian.com/applinks-api/3.2/com/atlassian/applinks/api/ApplicationLinkRequestFactory.html#createRequest(com.atlassian.sal.api.net.Request.MethodType,%20java.lang.String)) 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?


Answers:
author: Peter-Dave Sheehan
body: 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
)
```


author: Hana Ku?erov?
body: Hi [@Gleb Kartashov](/t5/user/viewprofilepage/user-id/3109905) ,

have you tried to use

```
def authenticatedRequestFactory = confluenceLink.createImpersonatingAuthenticatedRequestFactory()
```

?

See [here](https://scriptrunner.adaptavist.com/6.20.0/jira/interacting-with-confluence-from-jira.html) for more info.


Tags: authentication

confluence-server

groovy

server