Can you authenticate an api request from a user macro with the confluence credentials?

Maike August 1, 2022

Hi,

Currently I am developing a macro for a special user search. On the frontend you have the macro that runs in its current state as planed. The problem arises with the backend. The search is handled via an api which is in development.

On one hand you are able to get the current useres credentials inside the macro, so could the api check the authentication against the confluence instance?

Or is there a better way?

 

Thanks!

Please feel free to ask if I didn't make something clear. 

1 answer

0 votes
Ken McClean
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 4, 2023

You can add an authentication header. Here's a basic REST request macro script that includes the header.

Keep in mind that you may run into an error with CORS:

https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS/Errors/CORSDidNotSucceed?utm_source=devtools&utm_medium=firefox-cors-errors&utm_campaign=default


## Basic REST call with authentication in a Confluence Macro
## @noparams

<script type="text/javascript">
AJS.toInit(function() {

jQuery.ajax({
url: "<url>",
type: "GET",
contentType: "application/json",
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", "Basic " + btoa("username:password"));
},
success: function(data) {
console.log("HTTP request was successful.");
console.log(data);
},
error: function(xhr, status, error) {
console.log("HTTP request failed. Error message: " + error);
}
});

});
</script>

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events