Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to pass basic authentication using REST API in Confluence

Aashutosh Kumar
Contributor
January 8, 2021

Hi All,

I am trying to use REST API Calls to Confluence (Data Center version), but cannot figure out how to pass basic authentication in request call.

Below is example code taken from Confluence REST API Documentation.

import requests 
import json
url
= "https://your-domain.atlassian.net/wiki/rest/api/content"
headers
= { "Accept": "application/json" }
response
= requests.request( "GET", url, headers=headers ) print(json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ")))

 

 Below is the python request module code I am using.

import requests
import json
from requests.auth import HTTPBasicAuth
auth = HTTPBasicAuth("username","password")
url = "http://confluenceurl:8090/confluence/rest/api/content"
headers = {"Accept": "application/json"}
response = requests.request("GET",url,headers=headers,auth=auth)
print(json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ")))

But, I get 404 Response code
(404=Returned if the calling user does not have permission to view the requested content.A schema has not been defined for this response code.)

 

Thanks in Advance for helping and teaching.

 

1 answer

1 accepted

0 votes
Answer accepted
Prince Nyeche
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 8, 2021

Hi @Aashutosh Kumar 

Your headers has to be content-type.you can simply use below.

 

import requests
import json
from requests.auth import HTTPBasicAuth
auth = HTTPBasicAuth("username","password")
url = "http://confluenceurl:8090/confluence/rest/api/content"
headers = {"Content-type": "application/json"}
response = requests.request("GET",url,headers=headers,auth=auth)
print(json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ")))


Aashutosh Kumar
Contributor
January 8, 2021

Hi @Prince Nyeche ,

Thank you the response, I modified header data, still same response.

 

>>> headers = {"Content-type": "application/json"}
>>> requests.request("GET",url,headers=headers,auth=('user','passwd'))
<Response [404]>
>>>
>>> headers = {"Content-type": "application/json"}
>>> requests.request("GET",url,headers=headers,auth=auth)
<Response [404]>
>>>
Prince Nyeche
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 8, 2021

I think the issue is not the authentication but most probably the endpoint being used. Is that endpoint url accessible via the browser?

Aashutosh Kumar
Contributor
January 8, 2021

No, I tried below two urls.

http://jirasdconfluence.technicolor.com:8090/confluence/rest/api/content
http://jirasdconfluence.technicolor.com:8090/wiki/rest/api/content

I am using Confluence Data Center on own premises.


Page Not Found.

url.PNG

Prince Nyeche
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 8, 2021

There's the problem. the endpoint doesn't search any space. Please use 

http://jirasdconfluence.technicolor.com:8090/wiki/rest/api/content/search?cql=space=NAME_OF_A_SPACE

This is explained here on Advanced searching with CQL.

Aashutosh Kumar
Contributor
January 11, 2021

Hi @Prince Nyeche ,

I am still unable to understand, which url should i make call using REST API.
I want to fetch some data or post any data to any space/page in Confluence using REST API, but don't know how to pass user authentication.

None of the URLs are working.

I am using Confluence Data Center Version.

>>>url='http://jirasdconfluence.technicolor.com:8090/wiki/rest/api/content/search?cql=space=TECHPST'
>>> headers = {"Accept": "application/json"}
>>> requests.request("GET",url,headers=headers,auth=auth)
<Response [404]>
>>>

image.png


 

Prince Nyeche
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 11, 2021

Hi @Aashutosh Kumar 

I think you're using the directory wrong. If your confluence is not installed in any directory, it should be like 

http://jirasdconfluence.technicolor.com:8090/rest/api/content/search?cql=space=TECHPST'

"wiki" -> is a directory mostly used by Cloud Infrastructure for confluence. So try the above url and see if you can get the content of that space. 

Aashutosh Kumar
Contributor
January 11, 2021

It worked, Thank you so much @Prince Nyeche .

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events