Confluence API Token - pure JS
Asked by Wojciech Gwardjak:

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));
```


Answers:
author: Pramodh M
body: Hi [@Wojciech Gwardjak](/t5/user/viewprofilepage/user-id/4708378)

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

comments: author: Kishan Sharma
body: Hi [@Wojciech Gwardjak](/t5/user/viewprofilepage/user-id/4708378) Welcome to the Atlassian Community!  

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


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

![page body.PNG](https://community.atlassian.com/t5/image/serverpage/image-id/178050i46E5349BDA9115E6/image-size/large?v=v2&px=999 "page body.PNG")


author: Wojciech Gwardjak
body: Hi [@Kishan Sharma](/t5/user/viewprofilepage/user-id/554991) [@Pramodh M](/t5/user/viewprofilepage/user-id/3691224)

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](https://community.atlassian.com/t5/image/serverpage/image-id/178057i023126023A7C964D/image-size/large?v=v2&px=999 "content body.PNG")

The same occurs using space request, it pulls the pages but without body, I cannot pull the content :(  
![space content.png](https://community.atlassian.com/t5/image/serverpage/image-id/178066i3816C2A348412987/image-size/large?v=v2&px=999 "space content.png")


author: Kishan Sharma
body: Hi [@Wojciech Gwardjak](/t5/user/viewprofilepage/user-id/4708378)

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>  


author: Wojciech Gwardjak
body: Thank you [@Kishan Sharma](/t5/user/viewprofilepage/user-id/554991)

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.PNG](https://community.atlassian.com/t5/image/serverpage/image-id/178242iB8C8411D846F9A76/image-size/large?v=v2&px=999 "cors.PNG")![response0.PNG](https://community.atlassian.com/t5/image/serverpage/image-id/178241i15EEE2DAFD886103/image-size/large?v=v2&px=999 "response0.PNG")


author: Kishan Sharma
body: 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.


author: Pramodh M
body: Following to [@Kishan Sharma](/t5/user/viewprofilepage/user-id/554991) Answer thread here

<https://community.atlassian.com/t5/Confluence-questions/Confluence-API-Token-pure-JS/qaq-p/1911059#M230891>

Please find the reference below

<https://developer.atlassian.com/cloud/jira/platform/rest/v3/intro/#special-request-headers>

Thanks,  
Pramodh


author: Wojciech Gwardjak
body: Thanks for response [@Kishan Sharma](/t5/user/viewprofilepage/user-id/554991) [@Pramodh M](/t5/user/viewprofilepage/user-id/3691224)   
Unfortunately it still pulls the same response 0 :(

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

![no-cors.PNG](https://community.atlassian.com/t5/image/serverpage/image-id/178257i4615500761C86DFC/image-size/large?v=v2&px=999 "no-cors.PNG")![postman method.PNG](https://community.atlassian.com/t5/image/serverpage/image-id/178258iD778C43598431956/image-size/large?v=v2&px=999 "postman method.PNG")  
![response 0000.PNG](https://community.atlassian.com/t5/image/serverpage/image-id/178255iE64DDAF67AA76B42/image-size/large?v=v2&px=999 "response 0000.PNG")


author: Kishan Sharma
body: So you are now trying to embed the confluence page into your website ? If yes, I would suggest going through [this thread](https://community.atlassian.com/t5/Confluence-questions/How-do-I-embed-Confluence-pages-on-another-site/qaq-p/1730387) for more information.


Tags: api

cloud

confluence-cloud

token