- Created by Lasse Langhorn (Unlicensed), last modified on Sept 19, 2019
You are viewing an old version of this page. View the current version.
Compare with Current View Page History
« Previous Version 2 Next »
The Python code examples listed below can be saved as a file and executed via Python. You should change the following variables:
CONFLUENCE_USERNAME
CONFLUENCE_PASSWORD
CONFLUENCE_BASEURL
To alter the search change these variables:
CONFLUENCE_USER_EXPORT_SEARCH_TERM
CONFLUENCE_USER_EXPORT_ACTIVE_USERS
CONFLUENCE_USER_EXPORT_INACTIVE_USERS
CONFLUENCE_USER_EXPORT_OFFSET
CONFLUENCE_USER_EXPORT_PAGESIZE
It possible to use Python 2.7 or Python 3 without so please use the Python code that will suite your needs.
# -*- coding: utf-8 -*- ''' Creates a full Confluence user export as CSV 1. IMPORTANT: should be executed with Python 2.7 interpreter 2. Execution from terminal: python create_full_export_v1_python2_7.py > confluence_users.csv 3. A new file named confluence_users.csv is now available ''' import urllib import urllib2 import json import base64 # Change these variables CONFLUENCE_USERNAME = "admin" CONFLUENCE_PASSWORD = "admin" CONFLUENCE_BASEURL = "http://localhost:1990/confluence" # Search variables # eg. "admin" or "admin@admin.com" CONFLUENCE_USER_EXPORT_SEARCH_TERM = "" # True if enabled users is to be searched CONFLUENCE_USER_EXPORT_ACTIVE_USERS = True # True if disabled users is to be searched CONFLUENCE_USER_EXPORT_INACTIVE_USERS = True # Starting offset is 0 CONFLUENCE_USER_EXPORT_OFFSET = 0 # Pagesize. 100 is max page size CONFLUENCE_USER_EXPORT_PAGESIZE = 50 CONFLUENCE_USER_EXPORT_SEARCH = "/rest/confluenceuserexport/1.0/search" CONFLUENCE_USER_EXPORT_CSV_SERVLET = "/plugins/servlet/confluenceuserexport/admin/csv" JSON_MIME_TYPE = "application/json" BASIC_AUTH = "Basic " + base64.b64encode('%s:%s' % (CONFLUENCE_USERNAME, CONFLUENCE_PASSWORD)) all_users = [] def post_search(search_string, active_users, inactive_users, page_size, offset): ''' Search for Confluence user with the given parameters :param search_string: the given search string :param active_users: true if active users :param inactive_users: true if inactive users :param page_size: the given page size eg. 20 :param offset: the given offset eg. 0 :return: None ''''' header = {"Content-type": JSON_MIME_TYPE, "Accept": JSON_MIME_TYPE, "Authorization": BASIC_AUTH} url = CONFLUENCE_BASEURL + CONFLUENCE_USER_EXPORT_SEARCH body = {"searchString":search_string, "activeUsers":active_users, "inActiveUsers":inactive_users, "pageSize":page_size, "offset":offset } json_body = json.dumps(body) post_request = urllib2.Request(url, json_body, header) post_request.get_method = lambda: 'POST' try: post_request_open = urllib2.urlopen(post_request) post_response = post_request_open.read() if post_response: json_response = json.loads(s=post_response) if json_response: found_users = json_response.get("users") # Check for next page of users in order to search for more users has_next_page = json_response.get("hasNextPage") for found_user in found_users: all_users.append(found_user) if has_next_page: post_search(search_string, active_users, inactive_users, page_size, (offset + page_size)) except urllib2.HTTPError as e: print(e.getcode()) def post_csv(users_as_json): ''' Send JSON content to CSV servlet :param users_as_json: the Confluence users as JSON :return: None ''' request_headers = {"Content-type": JSON_MIME_TYPE, "Accept": JSON_MIME_TYPE, "Authorization": BASIC_AUTH} request_url = CONFLUENCE_BASEURL + CONFLUENCE_USER_EXPORT_CSV_SERVLET request_parameters = {'confluenceuserexport_download':'false','confluenceuserexport_users':users_as_json} post_request = urllib2.Request(url=request_url + "?" + urllib.urlencode(request_parameters), headers=request_headers) post_request.get_method = lambda: 'POST' try: post_request_open = urllib2.urlopen(post_request) post_response = post_request_open.read() if post_response: print(post_response) except urllib2.HTTPError as e: print(e.getcode()) # Start searching post_search(search_string=CONFLUENCE_USER_EXPORT_SEARCH_TERM, active_users=CONFLUENCE_USER_EXPORT_ACTIVE_USERS, inactive_users=CONFLUENCE_USER_EXPORT_INACTIVE_USERS, page_size=CONFLUENCE_USER_EXPORT_PAGESIZE, offset=CONFLUENCE_USER_EXPORT_OFFSET) # Call CSV servlet for conversion all_users_json = json.dumps(all_users) post_csv(all_users_json)
# -*- coding: utf-8 -*- ''' Creates a full Confluence user export as CSV 1. IMPORTANT: should be executed with Python 3 interpreter 2. Execution from terminal: python3 create_full_export_csv_python3.py > confluence_users.csv 3. A new file named confluence_users.csv is now available ''' from urllib import request, parse, error import json import base64 # Change these variables CONFLUENCE_USERNAME = "admin" CONFLUENCE_PASSWORD = "admin" CONFLUENCE_BASEURL = "http://localhost:1990/confluence" # Search variables # eg. "admin" or "admin@admin.com" CONFLUENCE_USER_EXPORT_SEARCH_TERM = "" # True if enabled users is to be searched CONFLUENCE_USER_EXPORT_ACTIVE_USERS = True # True if disabled users is to be searched CONFLUENCE_USER_EXPORT_INACTIVE_USERS = True # Starting offset is 0 CONFLUENCE_USER_EXPORT_OFFSET = 0 # Pagesize. 100 is max page size CONFLUENCE_USER_EXPORT_PAGESIZE = 50 CONFLUENCE_USER_EXPORT_SEARCH = "/rest/confluenceuserexport/1.0/search" CONFLUENCE_USER_EXPORT_CSV_SERVLET = "/plugins/servlet/confluenceuserexport/admin/csv" JSON_MIME_TYPE = "application/json" BASIC_AUTH = "Basic " + base64.b64encode(bytes('%s:%s' % (CONFLUENCE_USERNAME, CONFLUENCE_PASSWORD), 'utf-8')).decode('utf-8') all_users = [] def post_search(search_string, active_users, inactive_users, page_size, offset): ''' Search for Confluence user with the given parameters :param search_string: the given search string :param active_users: true if active users :param inactive_users: true if inactive users :param page_size: the given page size eg. 20 :param offset: the given offset eg. 0 :return: None ''''' request_headers = {"Content-type": JSON_MIME_TYPE, "Accept": JSON_MIME_TYPE, "Authorization": BASIC_AUTH} request_url = CONFLUENCE_BASEURL + CONFLUENCE_USER_EXPORT_SEARCH request_body = {"searchString":search_string, "activeUsers":active_users, "inActiveUsers":inactive_users, "pageSize":page_size, "offset":offset } post_request_json_bytes = json.dumps(request_body).encode("utf-8") post_request = request.Request(request_url, headers=request_headers) post_request.get_method = lambda: 'POST' try: post_request_open = request.urlopen(post_request, data=post_request_json_bytes) post_response = post_request_open.read() if post_response: json_response = json.loads(s=post_response) if json_response: found_users = json_response.get("users") # Check for next page of users in order to search for more users has_next_page = json_response.get("hasNextPage") for found_user in found_users: all_users.append(found_user) if has_next_page: post_search(search_string, active_users, inactive_users, page_size, (offset + page_size)) except error.HTTPError as e: print(e.getcode()) def post_csv(users_as_json): ''' Send JSON content to CSV servlet :param users_as_json: the Confluence users as JSON :return: None ''' request_headers = {"Content-type": JSON_MIME_TYPE, "Accept": JSON_MIME_TYPE, "Authorization": BASIC_AUTH} request_url = CONFLUENCE_BASEURL + CONFLUENCE_USER_EXPORT_CSV_SERVLET request_parameters = {'confluenceuserexport_download':'false','confluenceuserexport_users':users_as_json} post_request = request.Request(url=request_url + "?" + parse.urlencode(request_parameters), headers=request_headers) post_request.get_method = lambda: 'POST' try: post_request_open = request.urlopen(post_request) post_response = post_request_open.read() if post_response: print(post_response.decode("utf-8")) except error.HTTPError as e: print(e.getcode()) # Start searching post_search(search_string=CONFLUENCE_USER_EXPORT_SEARCH_TERM, active_users=CONFLUENCE_USER_EXPORT_ACTIVE_USERS, inactive_users=CONFLUENCE_USER_EXPORT_INACTIVE_USERS, page_size=CONFLUENCE_USER_EXPORT_PAGESIZE, offset=CONFLUENCE_USER_EXPORT_OFFSET) # Call CSV servlet for conversion all_users_json = json.dumps(all_users) post_csv(all_users_json)
- No labels