5.Use credentials

You need to have an active credential added to add-ons configuration if you don't have it please visit Add and activate credentia

Goal

Get number of Confluence spaces using a Confluence admin credential and compare it to same result without authentication.

0.Before we start

We created a credential with the key "myCredential" and we activated that. in our case since we're trying to authenticate as a Confluence admin, username and password should belong to a confluence admin

1.Confluence REST API to list Spaces

To see how we can use Credential to authenticate and get data we will use Confluence REST API, our Confluence running on a localhost using port 8090 you need to replace the base-url (http://localhost:8090) with your Confluence instance base-url

to get list of spaces we will send request to http://localhost:8090/rest/api/space the result will be something like this, we assumed we have less than 25 spaces which is the request page size limit to make it simple.

{ results: [ { id: 98305, key: "ds", name: "Demonstration Space", type: "global", _links: { webui: "/display/ds", self: "http://localhost:8090/rest/api/space/ds" }, _expandable: { metadata: "", icon: "", description: "", homepage: "/rest/api/content/65547" } }, { id: 98306, key: "SPAC", name: "SPACE", type: "global", _links: { webui: "/display/SPAC", self: "http://localhost:8090/rest/api/space/SPAC" }, _expandable: { metadata: "", icon: "", description: "", homepage: "/rest/api/content/65576" } } ], start: 0, limit: 25, size: 2, _links: { self: "http://localhost:8090/rest/api/space", base: "http://localhost:8090", context: "" } }

2.Use Credential

Create a new Confluence page (or edit and existing one) and add JavaScript Runner macro with following script inside its body.

var spacesWithCredential=JSON.parse(http.get("http://localhost:8090/rest/api/space","myCredential"));
var spacesWithoutCredential=JSON.parse(http.get("http://localhost:8090/rest/api/space"));
out.element("h3","Space count:"+spacesWithCredential.size);
out.element("h3","Space count:"+spacesWithoutCredential.size);

LINE DESCRIPTIONS:

1: Call the url using http object get function with "myCredential" as credential key and parse the result into a JSON object at the same time

2: Call the same url using http object get function without passing credential key and parse it to a JSON object

3,4: Print out the size of results for both using out object element function

3. Preview

As you see in the screen shoot the first output is 2 and second one is 0 since we didn't pass any credential to authenticate.