Confluence API Token - pure JS

Wojciech Gwardjak January 14, 2022

Hello everyone

I want to connect to Confluence space using API Token
I want to display data on my website using pure JS

 

First of all, I am stuck on generating proper access token as I am trying this method to work 

I got token from my customer which is 24 characters long
and it doesn't work using method below, 
Is there a way to generate API Token for Confluence only?

fetch('https://your-domain.atlassian.net/wiki/rest/api/content', { 
method
: 'GET',
headers
: {
'Authorization': 'Bearer <access_token>',
'Accept': 'application/json' } }
).then(response => {
console.log( `Response: ${response.status} ${response.statusText}` );
return response.text(); }
).then(text => console.log(text))
.catch(err => console.error(err));

 

1 answer

2 votes
Pramodh M
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 14, 2022

Hi @Wojciech Gwardjak 

Welcome to the community 🙂

You need Email and Token for Authentication 

Please find the ref here for converting both email and token to autothirzed header 

https://developer.atlassian.com/cloud/jira/platform/basic-auth-for-rest-apis/#supply-basic-auth-headers

Thanks,

Pramodh

Kishan Sharma
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 14, 2022

Hi @Wojciech Gwardjak Welcome to the Atlassian Community!

In your Authorization header, try replacing Bearer with Basic followed by the encoded string and see if that works.

Like # people like this
Wojciech Gwardjak January 17, 2022

Hello, thank you! It works but now I cannot get the page content body....
How to pull this data?

 

page body.PNG

Wojciech Gwardjak January 17, 2022

Hi @Kishan Sharma @Pramodh M 

Do you happen to know maybe how to make page content body visible?
In each page I receive request for I have blank space in body, while there is certainly content on my Confluence for each of them
Thank you!

content body.PNG

 

The same occurs using space request, it pulls the pages but without body, I cannot pull the content :(
space content.png

Kishan Sharma
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 18, 2022

Hi @Wojciech Gwardjak 

You can add below parameter to read content of a page with its body expanded.
 expand=body.storage

example - https://yoursite.atlassian.net/wiki/rest/api/content/12345678?expand=body.storage

Like Wojciech Gwardjak likes this
Wojciech Gwardjak January 18, 2022

Thank you @Kishan Sharma 

Actually it works!

But now the problem is.... 

While pulling the data with postman it sends object with the data and receives it well.

While doing the same on my page using JS function it responds with CORS error or response 0 (while mode: no-cors is on)

cors.PNGresponse0.PNG

Kishan Sharma
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 18, 2022

To call protected APIs from external systems you can add the X-Atlassian-Token header to each request, setting the value to no-check. Adding this header to a request bypasses the server-side XSRF check and allows the request to be fulfilled. 

-H "X-Atlassian-Token: no-check"

You can try adding the above header in your request and see if that works. 

Like # people like this
Pramodh M
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 18, 2022
Wojciech Gwardjak January 18, 2022

Thanks for response @Kishan Sharma  @Pramodh M 
Unfortunately it still pulls the same response 0 :( 

I use the same method for my page and the postman as well

no-cors.PNGpostman method.PNG
response 0000.PNG

Kishan Sharma
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 19, 2022

So you are now trying to embed the confluence page into your website ? If yes, I would suggest going through this thread for more information.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
TAGS
AUG Leaders

Atlassian Community Events