Forums

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

I need to 'remove' a user from our Jira Platform using the User Management APIs

Michael Savedra April 21, 2022

Hi!

I am building a script to bulk-handle the deprovisioning from our company Jira platform (using Python and REST API).

 

Using the 'Delete User' API from the REST API library I receive an error to say 'the user cannot be deleted as they have created issues and their records would be lots etc etc'

ref: https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-users/#api-rest-api-3-user-post

 

When I use the 'disable user' option from the User Management API, which I'm told requires the same level of access (Site admin privliges and an API token), I receive "{"code":401,"message":"Unauthorized"}"

I think it's just this bit i need to get working so was hoping someone could take a look at the code I'm using and let me know if there's something I'm missing here? 

 

my code for the 'Deactivate User' API call:

def disableUser():
global account_Id
url = "https://api.atlassian.com/users/" + account_Id + "/manage/lifecycle/disable"

headers = {
"Content-Type": "application/json",
"Authorization": "Bearer <my_api_token>"
}

payload = json.dumps( {
"message": "On 6-month suspension"
} )

response = requests.request(
"POST",
url,
data=payload,
headers=headers)

print(response.text)
Just to add, I can successfully execute all other calls from the REST API library, it seems to only be an issue when using the User Management API format with the 'Bearer' token (shown above) that returns the 401 error.
Please help! 
many thanks,
Mike

1 answer

0 votes
Neil Mansilla
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
April 22, 2022

@Michael Savedra just for clarity.. can you make successful calls to the GET (read) methods, such as Get Profile or Get API Tokens with your access token in the Authorization: Bearer xyz format?

Michael Savedra April 22, 2022

Hi Neil, 

Yes unfortunately it's the same story when I use the the api for Get Profile:

url = "https://api.atlassian.com/users/" + account_Id + "/manage/profile"
headers = {
"Accept": "application/json",
"Authorization": "Bearer <my-api-token>"
}
response = requests.request(
"GET",
url,
headers=headers)
print(json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ")))
This about returns the same "Unorthorized error 401"
However, when I use the get function from the general REST API library:
auth = HTTPBasicAuth("<my-email>", "<api-token>")

headers = {
"Accept": "application/json"}
response = requests.request(
"GET",
url,
headers=headers,
auth=auth)
global account_Id
print((json.loads(response.text)[0]["accountId"]))
account_Id = (json.loads(response.text)[0]["accountId"])
This returns the user's ID as it should. 
What could be causing issues using the Bearer formatt? I'm all out of ideas :( 
Michael Savedra April 25, 2022
Neil Mansilla
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
April 26, 2022

@Michael Savedra with those same credentials, can you perform the API call using a REST client (ex: Postman, Insomnia, etc.) successfully? This can help us get closer to understanding if this is a credentials issue or a Python/code problem.

Michael Savedra April 27, 2022

Hey @Neil Mansilla I'm able to to succfully execute the JIRA REST API on any commands from the main library. 

Do you have a link to an example for the postman/insomnia stuff so I could give that a go as well? 

Could it be that i'd need to be an Org Admin rather than Site Admin?

 

Cheers,

 

Michael

Michael Savedra May 5, 2022

@Neil Mansilla hey mate any thoughts?

Suggest an answer

Log in or Sign up to answer