Python REST API for accessing ticket information

Hi,

I have access to a RT system of an organization with a bunch of tickets. This can be accessed through a URL (the system resides in a virtual machine as a static snapshot of the actual system, this is for me to play with). I’m working on a project where I plan to automate several aspects of this particular system.

As a first step, there is a category called ticket type (1 of 6) for each ticket that gets assigned manually by the ticket handler based on the content of the incoming ticket email. I want to build a classifier to automatically classify the incoming ticket to one of the ticket type categories based on the ticket information which would include metadata of the ticket and contents of the email. Following this, I want to do some more stuff which would involve the entire ticket email chain including response from the user (I’m still working on what I want to do).

As a prerequisite to all this, I need to access all the ticket information (including metadata, all email contents etc.) which I can then parse to build machine learning models to achieve my goals. Essentially, I would like a data dump of the all the tickets in some form of file on disk (any sort of text file would be great). Following this, I could just parse and extract all the data that I need.

I am very new to the RT system (I’ve actually never used it), so I’m not sure how to go about doing this. I know very little about the REST API and I came across this link that seems to suggest that I could use some sort of script to do that for all the tickets. Again I’m not sure how to apply this to get what I want. Since I’m more knowledgeable in Python, I’d like to use an API in Python, although I could use other options if they are easier.

I’m looking for pointers/help/suggestions on how to go about getting data I want in the way I want it and would greatly appreciate any help.

Thanks!

We use the rt module on pypi → rt · PyPI it still works quite good, although the latest version is only on github at the moment → GitHub - python-rt/python-rt: Python interface to Request Tracker API

1 Like

I tried to work with 2 of the Python API’s, unfortunately the documentation is pretty slim and I couldn’t get them to work. I used the REST API documentation to get what I want. I’m still having problem and created a post on stackoverflow. I’ve not received any answers yet, so I thought I’d reproduce it here as well. Thanks and any help is appreciated.

We have a Request Tracker database set up on a virtual machine that I can access via Chrome. Access requires a username (mine) and keyfob password that changes every 30 seconds. I can access it in Chrome and after inputting my keyfob token once. I don’t need to do it again. I think it uses a session cookie.

I need to access this via command line (specifically through a python script) in order to get specific information on tickets for a project I’m working on. I went through the wiki on the REST API here. Specifically, I looked at the authentication part and the python access example part.

First I used the wget authentication shown :

wget --save-cookies cookies.txt --post-data 'user=username&password=keyfob_number' http://my.rt.server

I used the keyfob number that showed up at the time for my password (please note, I’m not sharing username and url for obvious reasons).

This didn’t work and threw an error:

--2017-06-25 11:08:11--  http://my.rt.server/
Resolving my.rt.server (my.rt.server)... xxx.xxx.xxx.xxx
Connecting to my.rt.server (my.rt.server)|xxx.xxx.xxx.xxx|:80... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://my.rt.server/ [following]
--2017-06-25 11:08:11--  https://my.rt.server/
Connecting to my.rt.server (my.rt.server)|xxx.xxx.xxx.xxx|:443... connected.
HTTP request sent, awaiting response... 401 Authorization Required

Username/Password Authentication Failed.

I also tried this via the Python (modified):

#!/usr/bin/env python

import sys
import cookielib
import urllib
import urllib2
import getpass

if __name__=='__main__':
    # creates a cookie for the rtserver with the credentials given at initialization.
    # define your credentials here
    access_user = raw_input('username: ')
    access_password = getpass.getpass()

    # here is the RequestTracker URI we try to access
    uri = 'http://my.rt.server/REST/1.0/'

    # trying login on rt server
    cj = cookielib.LWPCookieJar()
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
    urllib2.install_opener(opener)
    data = {'user': access_user, 'pass': access_password}
    ldata = urllib.urlencode(data)
    login = urllib2.Request(uri, ldata)
    try:
       response = urllib2.urlopen(login)
       print response.read()
       print "login successful"
    except urllib2.URLError:
       # could not connect to server
       print "Not able to login"

This also didn’t work and ended up with “Not able to login”. Also note that when I just visited “http://my.rt.server/REST/1.0/” on Chrome I get this:

RT/4.0.9 200 Ok

# Invalid object specification: ''

id: 

I’m not sure whether this might be a problem or not.

What I’m trying to is get very specific information (mainly the first message and the ticket type) about a bunch of tickets for processing later. I was able to figure what URL’s for each ticket to visit to get the information that I need. For example https://my.rt.server/REST/1.0/ticket/ticket-id/show for ticket type and a history URL for the first message. Essentially, I need to just save these HTML files which I can then parse to get the information I need.

I need help to solve this authentication problem so that I can just retrieve specific ticket data as HTML (or txt) files which can be saved on disk. Any help is appreciated and please let me know if you need extra information.

Thanks!

I looked at the IP you forget to redact in your post. It looks like there is some BasicAuth setup before the RT webserver. Maybe try this: HOWTO Fetch Internet Resources Using urllib2 — Python 2.7.18 documentation with your urllib2 script.