Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Confluence API Token - pure JS

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 Jan 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 Jan 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

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

 

page body.PNG

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 Jan 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

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 Jan 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

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 Jan 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
Community showcase
Published in Confluence Cloud

🏠 Say hello to the new Confluence Home!

Hi Atlassian Community, My name is DJ Chung, and I’m a Product Manager on the Confluence Cloud team. Today, I’m excited to share a new and improved version of Home. The new Home helps you ...

43,953 views 33 137
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you