Forums

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

Trouble adding users to the newly created project

Nawar Noorudeen January 3, 2024

I am having trouble adding new users to the project. Is it better to add users using their id or usernames? Below is my code:

if project_response.status_code == 201:
    print(f"Jira project '{project_name}' created successfully!")

    # Extract the project ID from the project creation response
    project_id = project_response.json()["id"]

    # Users to be added to the project
    users_to_add = ["xxx"]  # Add the usernames you want to add

    # Jira REST API endpoint for adding users to a project role
    role_id = "10195"  # Replace with the appropriate role ID
    add_users_url = f"{jira_url}/rest/api/3/project/{project_key}/role/{role_id}"

    # JSON payload for adding users to the project role
    users_payload = {
        "usernames": users_to_add
    }

    # Headers for the add users request
    users_headers = {
        "Content-Type": "application/json",
        "Accept": "application/json"
    }

    # Make the API call to add users to the project role
    users_response = requests.post(
        add_users_url,
        auth=HTTPBasicAuth(email, api_token),
        json=users_payload,
        headers=users_headers,
    )

    # Check the response status for adding users
    if users_response.status_code == 204:
        print("Users added to the project successfully!")
    else:
        print(f"Failed to add users to the project. Status code: {users_response.status_code}")
        print("Response content:")
        print(users_response.text)

else:
    print(f"Failed to create Jira project. Status code: {project_response.status_code}")
    print("Response content:")
    print(project_response.text)
The error I am recieving:
 Response content:
{"errorMessages":["Invalid request payload. Refer to the REST API documentation and try again."]}

2 answers

2 accepted

2 votes
Answer accepted
Marc - Devoteam
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.
January 3, 2024

Hi @Nawar Noorudeen 

If you use the API to add users, you are required to use ID's.

Cloud doesn't offer the option to use email address or user names as this is due to security.

Nawar Noorudeen January 3, 2024

@Marc - Devoteam Thank you!

0 votes
Answer accepted
Evgenii
Community Champion
January 3, 2024

Hi, @Nawar Noorudeen 

 

In list of users what exactly are you using, emails or ids?

According to REST API documentation, you must use user IDs.

Also, I don't see in documentation way to add list of users. You must add them one-by-one.

https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-project-role-actors/#api-rest-api-3-project-projectidorkey-role-id-post

Try to add one more loop, that will iterate through ids. Something like:

users = ["id1","id2","id3"]

for user_id in users:
users_payload = {
"user": user_id
}
...
...

 

Nawar Noorudeen January 3, 2024

I am trying to add one user at the moment to make this thing work. Is there any specific way to add the user id? 

I am passing it like this: 

users_to_add = ["712020:54f148d0-a991-4174-bbbe-369515"]
Evgenii
Community Champion
January 3, 2024

Here is my working code:

def add_role_actor(projectkey, userid, roleid):
url = f"{jira_api_url}/project/{projectkey}/role/{roleid}"
payload = json.dumps({
"user": [
userid
]
})
response = requests.post(url, headers = headers, data = payload)
if response.status_code == 200:
logger.info(f"User added to project: {projectkey}")

I suppose, you have error, because you don't make json.dumps for array with user data.

Like # people like this
Nawar Noorudeen January 3, 2024

@Evgenii Thank you for your help! I made some changes in my code and is working now! I really appreciate your help!

Like Evgenii likes this
Evgenii
Community Champion
January 3, 2024

Great. If answer helped you. please mark it as solution. It will help other people with similar problem

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
TAGS
AUG Leaders

Atlassian Community Events