It's all about the answers!

Ask a question

Authenticating to Jazz via Python


Kevin Murphy (9621317) | asked Oct 12 '11, 11:14 a.m.
All,

I'm trying to connect to Jazz via Python to do some OSLC testing. I found some old code here and have updated it a bit. I keep getting a 408 Server Timeout Error.

I'm wondering if anyone can give me some troubleshooting advice? Also, if I've posted to the wrong forum, please let me know.

Here's the code:


import urllib
import httplib2
from xml.dom.minidom import parse, parseString
from exceptions import Exception


class JazzClient(object):
def __init__(self, server_url, user, password):
self.base_url = server_url
self.jts_url = self.base_url + "/jts"
self.ccm_url = self.base_url + "/ccm"
self.qm_url = self.base_url + "/qm"

self.http = httplib2.Http()
self.http.disable_ssl_certificate_validation=True
self.http.follow_redirects = True
self.headers = {'Content-type': 'text/xml'}

#1) before authentification one needs to go first to a "restricted resource"
resp, content = self.http.request( self.ccm_url + "/oslc/workitems/1.xml", 'GET', headers=self.headers)
#TODO sometimes returns capital X-
#TODO check before if the key is in dictionary, if not something is wrong as well
if resp['x-com-ibm-team-repository-web-auth-msg'] != 'authrequired':
raise Exception("something is wrong seems the server doesn't expect authentication!")


self.headers['Cookie']= resp['set-cookie']
self.headers['Content-type'] = 'application/x-www-form-urlencoded'

#2 now we can start the authentication via j_security_check page
resp, content = self.http.request(self.jts_url+'/j_security_check' , 'POST', headers=self.headers,
body=urllib.urlencode({'j_username': user, 'j_password': password}))

#TODO check auth worked fine, if not throw exception
#if resp['x-com-ibm-team-repository-web-auth-msg'] != 'authrequired':
self.resp = resp
# raise Exception("something is wrong...seems the server doesn't like our credentials!")

#3 get the requested resource - finish the authentication
resp, content = self.http.request( self.ccm_url + "/oslc/workitems/1.xml", 'GET', headers=self.headers)

Accepted answer


permanent link
Colin Thorne (22421619) | answered Oct 21 '13, 12:20 p.m.
Although this is an old question this was the first hit I found on the forum when searching for python and authentication. Therefore I thought I would provide an answer in case anyone else comes across it.

I have just been trying to authenticate to jazz via python, and and the only part missing from the original code is to save the new cookie which is returned after the authentication. This seemed to be a change when we moved from RTC 3.1 to RTC 4.0 (in RTC 3.1 the cookie was the same from the first call).

#2 now we can start the authentication via j_security_check page

resp, content = self.http.request(self.jts_url+'/j_security_check' , 'POST', headers=self.headers, body=urllib.urlencode({'j_username': user, 'j_password': password}))
self.headers['Cookie']= resp['set-cookie'] # Save the new cookie returned here #3 get the requested resource - finish the authentication resp, content = self.http.request( self.ccm_url + "/oslc/workitems/1.xml", 'GET', headers=self.headers)

You'll notice that the curl command in another answer saves the cookie in both cases (using -b %COOKIES%) .

Hope this helps. Regards, Colin.
Daniel Toczala selected this answer as the correct answer

Comments
Daniel Toczala commented Nov 20 '13, 10:37 a.m. | edited Nov 20 '13, 10:39 a.m.
FORUM MODERATOR / JAZZ DEVELOPER

That did the trick for me.  Seems to work fine for me now.  I am using this to access an RTC server with Python, and I get data back the way that I would expect.

2 other answers



permanent link
Aaron Shepherd (37167) | answered Apr 03 '13, 3:13 p.m.
Hi Kevin,  Total shot in the dark on responding to this, but did you ever resolve your issue?  I am currently trying to do the same thing and am also running into 408 responses from my RQM server.

permanent link
sam detweiler (12.5k6195201) | answered Apr 03 '13, 7:47 p.m.
edited Apr 03 '13, 7:48 p.m.
I don't know python, but here is a short curl batch file that may help

<code>
set COOKIES=.\cookies.txt

set USER=
set PASSWORD=
set HOST=https://localhost:9743


curl -k -c %cookies% "%host%/jts/authenticated/identity"

curl -k -L -b %COOKIES% -c %COOKIES% -d j_username=%USER% -d j_password=%PASSWORD% "%host%/jts/authenticated/j_security_check"

curl -k -L -b %COOKIES% "%host%/ccm/oslc/workitems/catalog
</code>

Your answer


Register or 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.