Scriptrunner failing to execute confluence query using applink from Jira

Greg Bailey
Contributor
May 13, 2021

Hello,
Im attempting to connect to Confluence from Jira via applink leveraging the provided adaptivist solution at https://library.adaptavist.com/entity/create-a-confluence-page-for-each-subtask-of-an-issue but it is continually failing with a 'nonce_used' error when the cql call is executed.
I've compared the HTTP headers between Jira and confuence for the ouath calls and it all looks like it matches on both sides and at this point Im completely stuck.  I've also tried both applink config options of 'oauth' and 'oauth with impersonation' with the same failing result.


I really need to get this solutoin working since making rest calls using basic auth or an access token is a non-starter due to then needing to add the auth details in the script.

Can someone please help me to understand what may be going wrong here?

  • Jira Server: 8.5.3
  • Confluence Server: 7.5.2
  • Scriptrunner: 6.23

Code being executed from Scriptrunner console:

findConfluencePages()

static ApplicationLink createPrimaryConfluenceLink() {
def applicationLinkService = ComponentLocator.getComponent(ApplicationLinkService)

final def conflLink = applicationLinkService.getPrimaryApplicationLink(ConfluenceApplicationType.class)
conflLink
}

int findConfluencePages() {


def confluenceLink = createPrimaryConfluenceLink()
assert confluenceLink // must have a working app link set up

def authenticatedRequestFactory = confluenceLink.createImpersonatingAuthenticatedRequestFactory()


def responseBody
authenticatedRequestFactory
.createRequest(Request.MethodType.GET, "cql=type%3Dpage%20and%20space%3DAE%20and%20label%3D%22rsp-unreleased%22&expand=body")
.addHeader("Content-Type", "application/json")
.execute(new ResponseHandler<Response>() {
@Override
void handle(Response response) throws ResponseException {
if (response.statusCode != HttpURLConnection.HTTP_OK) {
throw new Exception(response.responseBodyAsString)
} else {
responseBody = new JsonSlurper().parseText(response.responseBodyAsString)
}
}
})

responseBody["id"] as Integer
}



 

Failing stack:

java.lang.Exception: oauth_problem=nonce_used at Script1167$1.handle(Script1167.groovy:44) at com.atlassian.applinks.oauth.auth.OAuthResponseHandler.handle(OAuthResponseHandler.java:48) at com.atlassian.plugins.rest.module.jersey.JerseyRequest$1.handle(JerseyRequest.java:115) at com.atlassian.plugins.rest.module.jersey.JerseyRequest$1.handle(JerseyRequest.java:113) at com.atlassian.plugins.rest.module.jersey.JerseyRequest$2.handle(JerseyRequest.java:134) at com.atlassian.sal.core.net.HttpClientRequest.executeAndReturn(HttpClientRequest.java:102) at com.atlassian.plugins.rest.module.jersey.JerseyRequest.executeAndReturn(JerseyRequest.java:131) at com.atlassian.plugins.rest.module.jersey.JerseyRequest.execute(JerseyRequest.java:113) at com.atlassian.applinks.core.auth.ApplicationLinkRequestAdaptor.execute(ApplicationLinkRequestAdaptor.java:50) at com.atlassian.applinks.oauth.auth.OAuthResponseHandler.handle(OAuthResponseHandler.java:46) at com.atlassian.plugins.rest.module.jersey.JerseyRequest$1.handle(JerseyRequest.java:115) at com.atlassian.plugins.rest.module.jersey.JerseyRequest$1.handle(JerseyRequest.java:113) at com.atlassian.plugins.rest.module.jersey.JerseyRequest$2.handle(JerseyRequest.java:134) at com.atlassian.sal.core.net.HttpClientRequest.executeAndReturn(HttpClientRequest.java:102) at com.atlassian.plugins.rest.module.jersey.JerseyRequest.executeAndReturn(JerseyRequest.java:131) at com.atlassian.plugins.rest.module.jersey.JerseyRequest.execute(JerseyRequest.java:113) at com.atlassian.applinks.core.auth.ApplicationLinkRequestAdaptor.execute(ApplicationLinkRequestAdaptor.java:50) at com.atlassian.applinks.oauth.auth.OAuthRequest.execute(OAuthRequest.java:71) at com.atlassian.sal.api.net.Request$execute$0.call(Unknown Source) at Script1167.findConfluencePages(Script1167.groovy:40) at Script1167.run(Script1167.groovy:15)

1 answer

Suggest an answer

Log in or Sign up to answer
0 votes
Deleted user June 17, 2021

@Greg Bailey 

can you try method may it will help you

 

def authenticatedRequestFactory = confluenceLink.createAuthenticatedRequestFactory()

def req = authenticatedRequestFactory.createRequest(GET,"cql=type%3Dpage%20and%20space%3DAE%20and%20label%3D%22rsp-unreleased%22&expand=body")

def handler = new ApplicationLinkResponseHandler<Map>() {
@Override
Map credentialsRequired(Response response) throws ResponseException {
return null
}

@Override
Map handle(Response response) throws ResponseException {
assert response.statusCode == 200
new JsonSlurper().parseText(response.getResponseBodyAsString()) as Map
}
}

def sessionDetails = req.execute(handler)
log.warn("Making the request as: " + sessionDetails)
def json_str = JsonOutput.toJson(sessionDetails)
def jsonSlurper = new JsonSlurper()
def cfg = jsonSlurper.parseText(json_str)
assert cfg instanceof Map
def id = cfg.results['id']

return id

TAGS
AUG Leaders

Atlassian Community Events