Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Python Script to bulk add users to group via API does not work

Carlos Sánchez April 16, 2024

I have followed the steps at https://confluence.atlassian.com/jirakb/bulk-add-users-to-groups-using-python-and-api-1299910891.html, which is a script provided by Atlassian to add users in bulk to groups.

 

I've followed every single step, but when running the script, I keep getting an error:

 

user_id = row['userid'] # Change to match your CSV column names
KeyError: 'userid'

 

 

This is the script as I am running it (private values not included of course):

 

#############################

import requests
from requests.auth import HTTPBasicAuth
import json
import csv

# Jira Cloud URL - update this value to your cloud url
jira_url = "https://xxxxxxxxxxxxxxxxxxx.atlassian.net/"

# API endpoint for adding users to a group
api_endpoint = f"{jira_url}/rest/api/3/group/user"

# Authentication credentials - update the email and api_token for your user and token combination
email = "xxxxxxxxxxxxxxxxxxxxx
api_token = "xxxxxxxxxxxxxxxxxxx"

# Read the CSV file with user and group IDs
csv_file_path = 'C:/Users/xxxxxxxxxDownloads/users_and_groups.csv'

auth = HTTPBasicAuth(email, api_token)

headers = {
"Accept": "application/json",
"Content-Type": "application/json",
}

# Process the CSV file
with open(csv_file_path, mode="r") as csv_file:
csv_reader = csv.DictReader(csv_file)
for row in csv_reader:
user_id = row['userid'] # Change to match your CSV column names
group_id = row['groupid'] # Change to match your CSV column names

# Construct the JSON payload
payload = json.dumps({"accountId": user_id})

# Set the query parameters for the group ID
query = {"groupId": group_id}

# Make the API request to add the user to the group
response = requests.post(
api_endpoint,
data=payload,
headers=headers,
params=query,
auth=auth,
)

if response.status_code == 201:
print(f"Added user with ID '{user_id}' to group with ID '{group_id}'.")
else:
print(
f"Failed to add user with ID '{user_id}' to group. Status Code: {response.status_code}"
)

###################################

 

 

The column in my .CSV file is names exactly 'userid' so I have no idea why this is happening.

 

Any ideas?

0 answers

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events