Jazz Forum Welcome to the Jazz Community Forum Connect and collaborate with IBM Engineering experts and users

How to add a user/roles to DNG programmatically through API? Using Python requests

 I've been running into issues as to how to add users through the API. I've been looking at this website: https://jazz.net/wiki/bin/view/Main/DraftTeamProcessRestApi#Members_collection and specifically the section "Members collection: POST". I've copied the data in the request body example and edited it to fit my services. I then added it to an xml file and as a string. I've tried posting both of them but I keep getting a 400 "If the request body is invalid" error. Could anyone point me in the right direction for this? 

s = requests.Session()
eaders3 = {
'Accept': 'application/rdf+xml',
'OSLC-core-version': '2.0',
'Content-Type': 'application/x-www-form-urlencoded',
#'Content-Type': 'application/xml;charset=UTF-8',
}

with open('pa-members-to-add.xml', 'rb') as f:
data = f.read().replace(b'\n', b'')

print(s.post(url3, headers=headers3, data=data))

I've tried changing data to data = xml as well. xml is a String """ with the edited request body example. 

0 votes

Comments

What version+ifix are you using?


You're trying to add a member to a project, with a role?

IME It's always best to get an API working using a REST client in a logged-in browser and only once that works move to coding reproducing exactly what you manually posted.

Please edit an example of a rejected body, precisely as POSTed by your code.

Also please show the precise URL you're posting to,  exactly as used by your s.post() call. - NOTE we do need the exact URL with all its parameters/encoding, because it's the combination of the URL, the headers and the body that's being rejected.

Unless you want to completely replace the project members you should first GET the existing members, then add an entry for the member you want to add.

Have you tried GETting the current members and checking the XML is as you expect? It's possible the current XML may have different tags/hierarchy or something - GETting the current members will mean you can check this.

 Also, you should be using header:


'Content-Type': 'application/xml;charset=UTF-8'
    
as shown on the page you link to - you are sending RDF XML

Yes I'm trying to add users and roles to an already created project. 

I've managed to log in manually as well as through code.  

The exact error I'm getting is "Request could not be parsed as XML."

The url is "https://website.name.com/rm/process/project-areas/_S-01wKEAEeujI9hRpLoqWg/members"

I also have used a GET to see the current xml and it matches with what I have input. I just did not show it in the code. 

I changed the Content type to what you said. I previously had it but I kept getting a 404 error. Now that I changed the print statement to output the error code though it's saying "User 'testuser1' was not found". So at this point it seems like I need to create a test user and then add them to the project I think. Do you know if the DNG API is able to create users or only add them to project? 

Yes, it is absolutely necessary that the user already exists in the server. You can use repotools commands e.g. -importUsers or -createUser or synchronize with LDAP. I am a ware of Java API that can be used, there is likely some WEB API to manage users, but I have not looked at that. The rest API explained in https://jazz.net/wiki/bin/view/Main/DraftTeamProcessRestApi  seems to be only to manage on a project area level. You would have to make sure the users exist, before you add them.



One answer

Permanent link

 The user must already exist. No AFAIK you can't create a user using this API - for Liberty user registry you can use repotools-jts -importUsers , see https://www.ibm.com/docs/en/elm/7.0.2?topic=reference-importusers or for LDAP the users must exist in LDAP and then I guess (haven't tried it) =importUsers can assign licenses.

1 vote

Your answer

Register or log in to post your answer.

Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.

Search context
Follow this question

By Email: 

Once you sign in you will be able to subscribe for any updates here.

By RSS:

Answers
Answers and Comments
Question details
× 10,936
× 478
× 233
× 116
× 10

Question asked: Jun 16 '22, 11:47 a.m.

Question was seen: 1,943 times

Last updated: Oct 19 '23, 2:28 p.m.

Confirmation Cancel Confirm