Authenticating to Rational Team Concert and Jazz REST Interfaces

I recently was trying to access RTC Team Concert Jazz REST Interfaces to create some custom reports from within a Python program I was writing.   The challenge was how do I authenticate with the REST Services.  There are 2 approaches you might have to take depending on how your Jazz Server is configured.

FORM Based Authentication
Basic Authentication

To figure out which of these authentication methods are being used on your installation check the RTC Jazz Tomcat or WebSphere Application Server.

To call URLs via Python you will need to import the URLLib2 .  I used this python example. Here is another great blog posting on using URLLib2.

FORM Based

FORM Based Is bit more involved.  You will likely have to keep track of which headers to set on each subsequent request you make once you have authenticated.

Here were the implementations that I was able to successfully get to work using Python:
The key to get this to work is to pass in a header with the user id and password that has been encoded.

base64string = base64.encodestring(
'%s:%s' % (feed.ds_uid, feed.ds_pw))[:-1]
authheader = "Basic %s" % base64string
req.add_header("Authorization", authheader)

You will be redirected once you authenticate and then you should pass in the cookie with your authenticated session on each subsequent HTTP request.

Complete  Python Code implementation:

COOKIEFILE = 'cookies.lwp'
# the path and filename to save your cookies in
cj = None
ClientCookie = None
cookielib = None
# Let's see if cookielib is available
try:
import cookielib
except ImportError:
# If importing cookielib fails
# let's try ClientCookie
try:
import ClientCookie
except ImportError:
# ClientCookie isn't available either
urlopen = urllib2.urlopen
Request = urllib2.Request
else:
# imported ClientCookie
urlopen = ClientCookie.urlopen
Request = ClientCookie.Request
cj = ClientCookie.LWPCookieJar()
else:
# importing cookielib worked
urlopen = urllib2.urlopen
Request = urllib2.Request
cj = cookielib.LWPCookieJar()
# This is a subclass of FileCookieJar
# that has useful load and save methods
if cj is not None:
# we successfully imported
# one of the two cookie handling modules
if os.path.isfile(COOKIEFILE):
# if we have a cookie file already saved
# then load the cookies into the Cookie Jar
cj.load(COOKIEFILE)
# Now we need to get our Cookie Jar
# installed in the opener;
# for fetching URLs
if cookielib is not None:
# if we use cookielib
# then we get the HTTPCookieProcessor
# and install the opener in urllib2
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(opener)
else:
# if we use ClientCookie
# then we get the HTTPCookieProcessor
# and install the opener in ClientCookie
opener = ClientCookie.build_opener(ClientCookie.HTTPCookieProcessor(cj))
ClientCookie.install_opener(opener)
txdata = None
# if we were making a POST type request,
# we could encode a dictionary of values here,
# using urllib.urlencode(somedict)
txheaders = {'User-agent' : 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'}
# fake a user agent, some websites (like google) don't like automated exploration
try:
req = Request(encoded_url, txdata, txheaders)
# create a request object encoding the userid and password and adding them to the headers.
base64string = base64.encodestring(
'%s:%s' % (feed.ds_uid, feed.ds_pw))[:-1]
authheader = "Basic %s" % base64string
req.add_header("Authorization", authheader)
handle = urlopen(req)
# and open it to return a handle on the url
data = handle.read()
#XML Document returned from the RTC REST Service.
print "Data returned %s" %data
return (data)
except IOError, e:
print 'We failed to open "%s".' % encoded_url
if hasattr(e, 'code'):
print 'We failed with error code - %s.' % e.code
elif hasattr(e, 'reason'):
print "The error object has the following 'reason' attribute :"
print e.reason
print "This usually means the server doesn't exist, is down, or we don't have an internet connection."
sys.exit()
else:
print 'Here are the headers of the page :'
print handle.info()
# handle.read() returns the page
# handle.geturl() returns the true url of the page fetched
# (in case urlopen has followed any redirects, which it sometimes does)
print
if cj is None:
print "We don't have a cookie library available - sorry."
print "I can't show you any cookies."
else:
print 'These are the cookies we have received so far :'
for index, cookie in enumerate(cj):
print index, ' : ', cookie
cj.save(COOKIEFILE)
# save the cookies again

Basic Authentication

Complete  Python Code implementation:

Code:
import urllib2
def rest_call( uid, pw, url):
print( "Entered rest_call" )
print( "url passed in was %s" %url)
password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_manager.add_password(
None, url, uid, pw
)
auth_handler = urllib2.HTTPBasicAuthHandler(password_manager)
opener = urllib2.build_opener(auth_handler)
urllib2.install_opener(opener)
try:
data = urllib2.urlopen(url).read()
except urllib2.HTTPError, e:
print( "HTTP error: %d" % e.code )
print( "HTTP error: %d" % e.reason )
print( "HTTP error: %s" % e.headers )
except urllib2.URLError, e:
print( "Network error: %s" % e.reason.args[1])
print("Here is what came back from the rest_call %s " %data)
return (data)

Resources

Programmatic authentication and certificate handling for Rational Team Concert 2.0.

Extending RTC Jazz.net Forums

Tech Note Jazz Team Server Authentication Explained.

Python URLLib2 Doug Hellmann’s blog:

Python URLLib2 Voidspace blog

Advertisements

About Carlos

Principal Technical Product Manager for Bluemix in IBM Software. The postings on these blogs are the authors own and don’t represent IBM’s positions, strategies or opinions.
This entry was posted in "Shared Experience", Advanced, Team Concert. Bookmark the permalink.

2 Responses to Authenticating to Rational Team Concert and Jazz REST Interfaces

  1. Pingback: Reporting Using Rational Team Concert | Getting to Smarter Systems Faster

  2. Pingback: Reporting Using Rational Team Concert | Getting to Smarter Systems Faster

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s