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'")
Hello Zhinoo,
Make sure that you have the latest version of the atlassian python api.
pip3 install --upgrade atlassian-python-api
Then try this code:
from atlassian import Confluence
import csv
confluence = Confluence(
url='https://yoursite.atlassian.net',
username = "your_email",
password = "your_token",
cloud=True)
spaces = confluence.get_all_spaces(start=0, limit=500, expand=None)
slist = spaces['results']
csv_filename = 'confluence_spaces.csv'
with open(csv_filename, mode='w', newline='') as csv_file:
csv_writer = csv.writer(csv_file)
csv_writer.writerow(['Key', 'Name'])
for s in slist:
csv_writer.writerow([s['key'], s['name']])
print(f'Data exported to {csv_filename}')
You can read the Atlassian Python API documentation here for more information.:
https://atlassian-python-api.readthedocs.io/
I hope it helps.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.