We have a Request Systematically generate a list of all Confluence Spaces & their Admins
for this i am trying to use the below python how much hard ever I try I am not getting the result always going though an error
import json
import requests
from requests.auth import HTTPBasicAuth
from openpyxl import Workbook
import logging
# Enable debugging for requests library
logging.basicConfig(level=logging.DEBUG, filename='requests_debug.log')
# Confluence Cloud API endpoint
BASE_URL = 'https://mygroup.atlassian.net/wiki'
# Authentication credentials
USERNAME = 'My User name'
API_TOKEN = 'API-TOKEN'
# Define headers with Basic authentication
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
}
# Define authentication credentials
auth = HTTPBasicAuth(USERNAME, API_TOKEN)
# Function to fetch all spaces and their admins
def fetch_space_admins():
try:
url = f'{BASE_URL}/rest/api/space?limit=5&expand=metadata,permissions' # Initial URL with limit parameter
all_spaces = []
while url:
response = requests.get(url, headers=headers, auth=auth)
if response.status_code == 200:
data = response.json()
print(json.dumps(data, indent=4)) # print response content for debugging
spaces = data.get('results', [])
all_spaces.extend(spaces)
# if data.get('_links', {}).get('next'):
# url = BASE_URL + data.get('_links', {}).get('next')
# else:
# url = None
url=""
print ("next url = " + url)
# if url:
# # ensure that the url has the correct format
# url = url['href']
else:
print(f'error: unable to fetch space admins. status code: {response.status_code}')
return
# Create Excel workbook and worksheet
wb = Workbook()
ws = wb.active
ws.append(['Space Name', 'Space Key', 'Administrators'])
# Iterate through spaces and fetch admins
for space in all_spaces:
space_name = space['name']
space_key = space['key']
if 'metadata' in space and 'administrators' in space['metadata']:
admins = ', '.join(space['metadata']['administrators'])
else:
admins = 'No administrators'
ws.append([space_name, space_key, admins])
# Save Excel workbook
wb.save('confluence_spaces.xlsx')
print('Spaces and administrators exported to confluence_spaces.xlsx')
except Exception as e:
print(f'Error: {str(e)}')
# Main function
def main():
fetch_space_admins()
if __name__ == "__main__":
main()
Can some one help me on this ?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.