Adding User to group

Sam November 28, 2024

 

import requests
from requests.auth import HTTPBasicAuth
import json
from urllib.parse import urlencode

def add_user_to_group(base_url, email, api_token, group_name, user_name):
    api_endpoint = f"{base_url}/rest/api/2/group/user"

    auth = HTTPBasicAuth(email, api_token)

    headers = {
        "Accept": "application/json",
        "Content-Type": "application/json",
        "Authorization": f"Bearer {API_TOKEN}"
    }

    payload = json.dumps({"name": user_name})

    query = {"groupname": group_name}

    response = requests.post(
        f"{api_endpoint}?{urlencode(query)}",
        headers=headers,
        data=payload,
        auth=auth
    )

    if response.status_code == 201:
        print(f"User '{user_name}' added to group '{group_name}' successfully.")
        print("Group details:", json.dumps(response.json(), indent=4))
    else:
        print(f"Failed to add user '{user_name}' to group '{group_name}'.")
        print(f"Status Code: {response.status_code}")
        print("Error message:", response.text)

if __name__ == "__main__":
    BASE_URL = "http://jira.com"
    EMAIL = "example@gmail.com"
    API_TOKEN = "123"
    GROUP_NAME = "Testing"
    USER_NAME = "123456"
   
    add_user_to_group(BASE_URL, EMAIL, API_TOKEN, GROUP_NAME, USER_NAME)     I am getting 401 error can anyone help me in this

1 answer

1 accepted

0 votes
Answer accepted
Florian Bonniec
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 28, 2024

Hi @Sam 

 

401 means permission error. You do not provide the right credentials. You should provide the credential as "basic {base64 encoded user:password}"

 

Regards

Sam November 28, 2024

Hi @Florian Bonniec I am using token right! so need of password and it has permission also for reference .I checked this one The Jira Service Management REST API

Florian Bonniec
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 28, 2024

Did you follow this ? Using Personal Access Tokens | Atlassian Support | Atlassian Documentation

 

You are using Token but also referencing HTTPBasicAuth

Sam November 28, 2024

yes,I tried without http basic auth also on that time i am getting 405 error

 

Florian Bonniec
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 28, 2024

This script works

 

import requests
import json

# Replace these variables with your JIRA instance details
groupName = ''
jira_url = ''
api_endpoint = f'/rest/api/2/group/user?groupname={groupName}'
url = jira_url + api_endpoint

# Your JIRA credentials
api_token = ''

# Headers for the request
headers = {
    'Content-Type': 'application/json',
    'Authorization': f'Bearer {api_token}'
}

# Data
data = {
  "name": ""
}

# Make the POST request to create the issue
response = requests.post(url, headers=headers, data=json.dumps(data), verify=False)

# Check the response
if response.status_code == 201:
    print("User Added successfully.")
    print("Response:", response.json())
else:
    print("Failed to add user.")
    print("Response:", response.status_code)

 

Sam November 28, 2024


Failed to add user.
Response: 405

Florian Bonniec
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 28, 2024

Most likely your base url is wrong. I tested it and works on my side.

Did you include the path if any ? like url/jira/rest....

Suggest an answer

Log in or Sign up to answer