Can't make curl access corporate confluence pages using API token (Basic auth works)
Asked by Kelly Maurice:

I'm having this problem. We have self-hosted confluence at my company. Basic authentication works with curl and username/pw:

curl -D- -u \<username\>:\<password\> -X GET -H "Content-Type: application/json" \<confluence page to browse\>

However, after getting a new API token from confluence website and substituting for the password:

curl -D- -u \<username\>:\<API token\> -X GET -H "Content-Type: application/json" \<confluence page to browse\>

it doesn't work - I get a 401 response and the following message included with other stuff:

Basic Authentication Failure - Reason : AUTHENTICATED_FAILED\</p\>\<p\>\<b\>Description\</b\> The request has not been applied to the target resource because it lacks valid authentication credentials for that resource.

It's curious that is used the words Basic Authentication Failure since I'm trying now to use API token and not Basic anymore.

I see answers online about:  
- using network name instead of username  
- leaving username blank (somehow)  
- using email instead of username (for Basic authorization only username works, not email)

But these don't seem to work (and I don't know how to get my network username)


Answers:
author: Jehan Bhathena
body: Hi [@Kelly Maurice](/t5/user/viewprofilepage/user-id/5457930)

Please can you share the API that you're using?

I tried this API and it works with the \<username\>:\<API token\> combination

Documentation : <https://developer.atlassian.com/cloud/confluence/rest/v2/api-group-space/#api-spaces-get>

comments: author: Kelly Maurice
body: Hi Jehan. In fact, I made it work on ubuntu, including remote ubuntu in a docker image. But it doesn't work on Windows. That's where I was stuck.

I am a little confused about which API to use: whether to use Server Confluence REST API or Cloud Conference REST API (and which version). I believe we host our own confluence at Here Technologies where I work. This is the command I used that worked on Ubuntu:  

*curl -u \<Here username:password\> -X POST -H 'Content-Type:application/json' -d '{"type":"page","title":"testing_curl_ubuntu","ancestors":\[{"id":1949495409}\],"space":{"key":"OneMapDev"},"body":{"storage":{"value":"\<p\>This is a new page\</p\>","representation":"storage"}}}' <https://confluence.in.here.com/rest/api/content/>*

So, that initial step worked on Linux but the next thing I am stuck on is trying to write the new page with content that includes an image (not as an attachment). It quietly fails and says

Oops - an error has occurred  

![logo](/images/logo/confluence-logo-adg3.svg "Confluence") System Error {#toc-hId--381078193}
----------------------------------------------------------------------------------------------

But it doesn't add the page.  

I modified the working initial command to create the blank page, and came up with this:  

curl -u username:pw -X POST -H 'Content-Type:application/json' -H 'Accept: application/json' -d '{"type":"page","title":"Sample_Page_With_Images","ancestors":\[{"id":1949495409}\],"space":{"key":"OneMapDev"},"body":{"storage":{"value":"\<p\>This is a new page\</p\>\<ac:image\>\<ri:attachment ri:filename=\\"sino.png\\","representation":"storage"}}}' <https://confluence.in.here.com/rest/api/content/>  

So, again I have the parent page underneath which I want to write a new child page. This parent is specified as an ID (1949495409). I basically tweaked my previous command to simply add the image (sino.png). But the ri:attachment seems to indicate it is trying to attach something. I only want to write the image directly into the doc as a rendered image.  

I based this code on a different form of the command I found in:  

<https://community.atlassian.com/t5/Confluence-questions/is-it-possible-to-upload-a-image-in-confluence/qaq-p/1254693>  

thanks


author: Kelly Maurice
body: The command was slightly wrong (no closing \</ac:image\>) but it didn't help:  

curl -u username:pw -X POST -H 'Content-Type:application/json' -H 'Accept: application/json' -d '{"type":"page","title":"Sample_Page_With_Images","ancestors":\[{"id":1949495409}\],"space":{"key":"OneMapDev"},"body":{"storage":{"value":"\<p\>This is a new page\</p\>\<ac:image\>\<ri:attachment ri:filename=\\"sino.png\\" /\>\</ac:image\>,"representation":"storage"}}}' <https://confluence.in.here.com/rest/api/content/>  

Some error details are given below:  

### Cause {#toc-hId-2116937779}

**Exception ID:** 7f2f8d55-4656-4d1c-91e5-5fc0329799e0

### Referer URL {#toc-hId-309483316}

Unknown


Tags: cloud

confluence-cloud