Forums

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

I can't get all the spaces using api on python

Zhinoo Zobairy September 12, 2023
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
Ansar Rezaei
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.
September 12, 2023 edited

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.

Suggest an answer

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

Atlassian Community Events