Although I am a confluence-admin, I still can't get the api using python. So here is my snippet:
import requestsimport csvimport base64# Define your Confluence Cloud API URLbase_url = "..."# Replace 'your-email' and 'your-api-token' with your email address and API tokenemail = "....."api_token = "...."# Create a list to store all spacesall_spaces = []# Initialize 'next' URL for paginationnext_url = base_urlwhile next_url:# Combine email and API token with a colon separatorcredentials = f"{email}:{api_token}"# Encode credentials using UTF-8 encodingcredentials_bytes = credentials.encode("utf-8")# Base64 encode the credentialscredentials_base64 = base64.b64encode(credentials_bytes).decode("utf-8")# Construct the "Authorization" header with the base64-encoded credentialsheaders = {"Authorization": f"Basic {credentials_base64}",}# Make the GET request to the next URLresponse = 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 pagesnext_url = space_data.get("next")else:print(f"Error: {response.status_code} - {response.text}")break# Create a CSV file and write the headerwith 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 filefor 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'")
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
It looks like you're new here. Sign in or register to get started.