Cannot authenticate request to my app in Confluence

Hello,

I am working with Atlassian Connect and Spring Boot. I am trying to make authenticated calls from the iframe back to my app in a Confluence Macro Editor but I simply cannot. I have just tried to replicate this example Bitbucket and while it works in Jira I cannot make it work in Confluence (error 401). Adding @IgnoreJwt to my app method allows the method to be executed so I am certain that the Ajax call is right.

Have anyone any tip or a working example of Ajax request in Confluence?

1 Like

Hi @ajdionisio,

As the example is a bit outdated I suspect you are running into the recent changes described in this topic. There’s a few steps you’ll need to take in order to get it working:

  • Set <atlassian-connect-spring-boot.version>2.1.0</atlassian-connect-spring-boot.version> to 2.1.5 or later in the pom.xml
  • Add the @ContextJwt annotation to the endpoint in the DataController
  • Add the following to your atlassian-connect.json (might actually not be required anymore):
  "apiMigrations": {
     "context-qsh": true
  }

EDIT: Sorry, I hadn’t looked at the JavaScript code in the repository and was blindly assuming it was using AP.context.getToken which it actually does not. To make what I wrote above work you’ll also need to adjust the JS in iframe.html:

$(document).ready(function() {
	$('#hello').click(function() {
		AP.context.getToken().then(function(token) {
			$.ajax({
				url: 'data',
				beforeSend: function(request) {
					request.setRequestHeader('Authorization', 'JWT ' + token);
				}
			}).done(function(data) {
				alert(JSON.stringify(data, null, '\t'));
			});
		});
	});
});

To be honest, I’m not 100% sure why it doesn’t work with the token that’s generated on the backend. But it might still be related to the context QSH changes.


After doing all of that, and re-installing your app into your instance, I think it should work. Hope this helps!

Cheers,
Sven

2 Likes

Thank you very much! Applying these changes make it work. I was lost because following the example I was unable to achieve it :slight_smile:

1 Like