Forums

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

Can't get the list of all spaces using the confluence api

Zhinoo Zobairy September 12, 2023
Although I am a confluence-admin, I still can't get the api using python. So here is my snippet:

import
requests

import csv

import base64

# Define your Confluence Cloud API URL

base_url = "..."

# Replace 'your-email' and 'your-api-token' with your email address and API token

email = "....."

api_token = "...."

# Create a list to store all spaces

all_spaces = []

# Initialize 'next' URL for pagination

next_url = base_url

while next_url:

# Combine email and API token with a colon separator

credentials = f"{email}:{api_token}"

# Encode credentials using UTF-8 encoding

credentials_bytes = credentials.encode("utf-8")

# Base64 encode the credentials

credentials_base64 = base64.b64encode(credentials_bytes).decode("utf-8")

# Construct the "Authorization" header with the base64-encoded credentials

headers = {

"Authorization": f"Basic {credentials_base64}",

}

# Make the GET request to the next URL

response = requests.get(next_url, headers=headers)

# Check if the request was successful (status code 200)

if response.status_code == 200:

space_data = response.json()

spaces = space_data["results"]

all_spaces.extend(spaces)

# Check if there are more pages

next_url = space_data.get("next")

else:

print(f"Error: {response.status_code} - {response.text}")

break

# Create a CSV file and write the header

with open("confluence_spaces_xom.csv", mode="w", newline="") as csv_file:

fieldnames = ["Space Key", "Space Name"]

writer = csv.DictWriter(csv_file, fieldnames=fieldnames)

writer.writeheader()

# Write space data to the CSV file

for space in all_spaces:

space_key = space["key"]

space_name = space["name"]

writer.writerow({"Space Key": space_key, "Space Name": space_name})

print("Spaces exported to 'confluence_spaces_xom.csv'")

1 answer

1 accepted

0 votes
Answer accepted
Fabio Racobaldo _Catworkx_
Community Champion
September 16, 2023

Hi @Zhinoo Zobairy and welcome,

there is a specific rest api to retrieve all space in your instance https://developer.atlassian.com/cloud/confluence/rest/v2/api-group-space/#api-spaces-get

Hope this helps,

Fabio

Suggest an answer

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

Atlassian Community Events