z4r / python-rtkit

Python Api for Request Tracker's REST interface
http://z4r.github.com/python-rtkit/
Other
68 stars 44 forks source link

create ticket having RESOURCE_STATUS: 401 Credentials required #38

Closed LingjingFrance closed 10 years ago

LingjingFrance commented 10 years ago

Hello ,

I'm new here . I searched for the similar post, but I don't find the answer .

I'm having trouble with a simple script to create a single ticket and every time I get [DEBUG] RESOURCE_STATUS: 401 Credentials required

The code in my script is:

#!/usr/bin/python -u
from rtkit.resource import RTResource
from rtkit.errors import RTResourceError
from rtkit.authenticators import BasicAuthenticator

from rtkit import set_logging
import logging

set_logging('debug')
logger = logging.getLogger('rtkit')
resource = RTResource('http://ticket.corp.kk.net/rt3/REST/1.0/', 'user', 'password', BasicAuthenticator)
#create a ticket
content = {
    'content': {
        'Queue': 'example@kelkoo.net', #General - unassigned is the name of the desired queue
        'Subject' : 'Test Ticket creation in RT with Python', #text to go into subject field
        }
    }
try:
    response = resource.post(path='ticket/new', payload=content,)
    logger.info(response.parsed)
except RTResourceError as e:
    logger.error(e.response.status_int)
    logger.error(e.response.status)
    logger.error(e.response.parsed)

The trace is as below:

[DEBUG] POST ticket/new
[DEBUG] {'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8', 'Accept': 'text/plain'}
[DEBUG] 'content=Queue: example@kelkoo.net\nSubject: Test Ticket creation in RT with Python'
[INFO] POST
[INFO] http://ticket.corp.kk.net/rt3/REST/1.0/ticket/new
[DEBUG] HTTP_STATUS: 200 OK
[DEBUG] 'RT/3.8.13 401 Credentials required\n'
[DEBUG] RESOURCE_STATUS: 401 Credentials required
[INFO] []
[INFO] []

Could someone help me for this ? Thanks a lot .

z4r commented 10 years ago

Could you try with CookieAuthenticator instead of BasicAuthenticator?

LingjingFrance commented 10 years ago

Thanks Andrea, I tried CookieAuthenticator (to create or read ticket) but I still get

 'RT/3.8.13 401 Credentials required 
. The code I used is https://github.com/z4r/python-rtkit#id9

I'm wondering if threre's something missing in my code ? Where can I store my cookie value so that it can be reused (or is this managed automatically by python-rtkit )?

When I load the url directly in the browser : http://ticket.corp.kk.net/REST/1.0/ticket/214560?user=user&pass=pass, I DO get the ticket content in the browser.

I even tried wget command to get one ticket content as below using cookie ,and it works well :

  1. save manually the cookie value in the coookie.txt file
    RT_SID_kk.net.80=5a1c1eb207c4e2ef5af726e98d751a08
    
  2. run this command :
    wget -O ticketContent.txt   --keep-session-cookies --save-cookies cookies.txt 'http://ticket.corp.kk.net/REST/1.0/ticket/220680/show?format=l&user=user&pass=pass' 
    

The ticket content is well registered in ticketContent.txt , which is showing using the cookie to authenticate is working.

How can I use CookieAuthenticator correctly in my script python ? Many thanks.

LingjingFrance commented 10 years ago

I saw the waring message: " With RT/3.8 you can't use ? inside the names of your custom fields, with RT/3.6 /() too." Does this mean the pytho-rtkit doesn't work with RT 3.8 ? (I use RT 3.8.13)

z4r commented 10 years ago

I spoke about chars: With RT/3.8 you can't use the char ? inside the names of your custom fields, with RT/3.6 the chars / ( and ) too.

LingjingFrance commented 10 years ago

I haven't found a solution for this problem , but I succeeded to use Requests library to create a ticket in RT .

  1. install Requests lib. http://docs.python-requests.org/en/latest/user/install/#install
  2. create a new ticket in RT
import requests,logging
logging.basicConfig(level=logging.DEBUG)
post_data = """
id: ticket/new
Queue: myqueue
Subject: Test Ticket creation in RT with Python
Text: Wow ticket is created :-D . 
"""

payload = {'user': 'user', 'pass': 'password','content':post_data}
ticket_creation_reusult = requests.post("http://ticket.corp.kk.net/rt3/REST/1.0/ticket/new", payload)
logging.debug(ticket_creation_reusult.text)

The output is:

INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): ticket.corp.kk.net
DEBUG:requests.packages.urllib3.connectionpool:"POST /rt3/REST/1.0/ticket/new HTTP/1.1" 200 None
DEBUG:root:RT/3.8.13 200 Ok
# Ticket 221173 created.

Hope this can help you , if you have the same problem as me . :-)

z4r commented 10 years ago

Hi @kelkoolingjing, I can't figure out which kind of RT installation uses the querystring authentication and not the cookie one.

Can you try the snippet below with my test credentials and then on you server?

rt.easter-eggs.org is a demo RT server, so feel free to use it.

from rtkit.resource import RTResource
from rtkit.errors import RTResourceError
from rtkit.authenticators import CookieAuthenticator

from rtkit import set_logging
import logging

set_logging('debug')
logger = logging.getLogger('rtkit')
resource = RTResource('http://rt.easter-eggs.org/demos/3.8/REST/1.0/', 'john.foo', 'john.foo', CookieAuthenticator)
content = {
    'content': {
        'Queue': 'Customer Service',
        'Subject' : 'Test Ticket creation in RT with Python',
        }
    }
try:
    response = resource.post(path='ticket/new', payload=content,)
    logger.info(response.parsed)
except RTResourceError as e:
    logger.error(e.response.status_int)
    logger.error(e.response.status)
    logger.error(e.response.parsed)
LingjingFrance commented 10 years ago

Hello Andrea (z4r),

Thank you for your answer and your help . I really appreciate it .
I tried your code , It does work (a new ticket is created ), but when I switch to our server , with correct credentials , I have the errors:

[DEBUG] 'RT/3.8.13 401 Credentials required\n' [DEBUG] RESOURCE_STATUS: 401 Credentials required I suppose this should related to our RT installation which uses the querystring authentication and not the cookie as you mentioned .
mgaulton commented 10 years ago

I'm having a similar issue in trying to get this working. I get 401 Creds...

This works with python-rt, but since it has issues with spaces in custom field names, I'm trying to switch to rtkit. Any suggestions would be appreciated.

import sys import re import smtplib import subprocess import os import rt import ConfigParser import time from rtkit.resource import RTResource from rtkit.authenticators import BasicAuthenticator from rtkit.errors import RTResourceError

from rtkit import set_logging import logging set_logging('debug') logger = logging.getLogger('rtkit')

config = ConfigParser.RawConfigParser() config.read(os.path.expanduser('~/.rtrcp'))

notkit rtpoint = "https://{0}/REST/1.0/".format(config.get('rt','hostname'))

rtuser = config.get('rt','username') rtpass = config.get('rt','password') rthost = config.get('rt','hostname')

resource = RTResource('http://{0}/REST/1.0/', '{1}', '{2}', BasicAuthenticator).format(rthost,rtuser,rtpass)

resource = RTResource('http://rt.uwaterloo.ca/REST/1.0/', 'rtuser', 'rtpass', BasicAuthenticator) print 'blah' try: resource = RTResource('http://rt.uwaterloo.ca/REST/1.0/', 'rtuser', 'rtpass', BasicAuthenticator)

except: print rtuser, rtpass, rthost print "Oops." sys.exit(0)

content = { 'content': { 'Queue': 'Incidents',#'', 2 'Subject': 'New Ticket Test', 'Text': 'My useless\ntext on\nthree lines.', } } try: response = resource.post(path='ticket/new', payload=content,) logger.info(response.parsed) except RTResourceError as e: logger.error(e.response.status_int) logger.error(e.response.status) logger.error(e.response.parsed)

[soc@basilisk ~]$ python relay_userv3.py blah [DEBUG] POST ticket/new [DEBUG] {'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8', 'Accept': 'text/plain'} [DEBUG] 'content=Queue: Incidents\nText: My+useless%0A+text+on%0A+three+lines.\nSubject: New Ticket Test' [INFO] POST [INFO] http://rt.uwaterloo.ca/REST/1.0/ticket/new [DEBUG] HTTP_STATUS: 200 OK [DEBUG] 'RT/4.2.9-43-gd7c0cfd 401 Credentials required\n' [DEBUG] RESOURCE_STATUS: 401 Credentials required [INFO] [] [INFO] []

z4r commented 10 years ago

Hi @kelkoolingjing and @mgaulton , I wrote a new authenticator for QueryString...but it's difficult for me to test it, because i've a different server installation.

Could you give me an help? You could clone the QueryStringAuth branch.

5395e0a5fc0931d85d1f0afd42bb121b037d7c86

from rtkit.resource import RTResource
from rtkit.authenticators import QueryStringAuthenticator
from rtkit.errors import RTResourceError

from rtkit import set_logging
import logging
set_logging('debug')
logger = logging.getLogger('rtkit')

resource = RTResource('http://<HOST>/REST/1.0/', '<USER>', '<PWD>', QueryStringAuthenticator)
content = {
    'content': {
        'Queue': 'Customer Service',
        'Subject' : 'Test Ticket creation in RT with Python',
        }
    }
try:
    response = resource.post(path='ticket/new', payload=content,)
    logger.info(response.parsed)
except RTResourceError as e:
    logger.error(e.response.status_int)
    logger.error(e.response.status)
    logger.error(e.response.parsed)

Thx

mgaulton commented 10 years ago

I have some progress but the ticket doesn't get created and no ticket id is returned.

[DEBUG] POST ticket/new [DEBUG] {'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8', 'Accept': 'text/plain'} [DEBUG] 'content=Queue: -general-\nText: My+useless%0A+text+on%0A+three+lines.\nSubject: New Ticket Test' [INFO] POST [INFO] http://rt.uwaterloo.ca/REST/1.0/ticket/new [DEBUG] HTTP_STATUS: 200 OK [DEBUG] 'RT/4.2.9-43-gd7c0cfd 200 Ok\n\n# Required: id, Queue\n\nid: ticket/new\nQueue: \nRequestor: issminion\nSubject: \nCc:\nAdminCc:\nOwner: \nStatus: new\nPriority: \nInitialPriority: \nFinalPriority: \nTimeEstimated: 0\nStarts: 2014-12-01 15:03:10\nDue: \nAttachment: \nText: \n\n' [DEBUG] RESOURCE_STATUS: 200 Ok [INFO] [[('id', 'ticket/new'), ('Queue', ''), ('Requestor', 'issminion'), ('Subject', ''), ('Cc', ''), ('AdminCc', ''), ('Owner', ''), ('Status', 'new'), ('Priority', ''), ('InitialPriority', ''), ('FinalPriority', ''), ('TimeEstimated', '0'), ('Starts', '2014-12-01 15:03:10'), ('Due', ''), ('Attachment', ''), ('Text', '')]] [INFO] [[('id', 'ticket/new'), ('Queue', ''), ('Requestor', 'issminion'), ('Subject', ''), ('Cc', ''), ('AdminCc', ''), ('Owner', ''), ('Status', 'new'), ('Priority', ''), ('InitialPriority', ''), ('FinalPriority', ''), ('TimeEstimated', '0'), ('Starts', '2014-12-01 15:03:10'), ('Due', ''), ('Attachment', ''), ('Text', '')]]

z4r commented 10 years ago

You're posting on Queue: -general-...are you sure?

mgaulton commented 10 years ago

yes, that's what they called the queue

On 12/1/14, Andrea de Marco notifications@github.com wrote:

You're posting on Queue: -general...are you sure?


Reply to this email directly or view it on GitHub: https://github.com/z4r/python-rtkit/issues/38#issuecomment-65145926

z4r commented 10 years ago

can you try with the id of the queue?

z4r commented 10 years ago

I think is something related to -, but we can open another issue for that!

mgaulton commented 10 years ago

I also tried using the 'Incidents' queue and had the same result. Any easy way to find the id for the queue?

LingjingFrance commented 10 years ago

Hello z4r,

Great job !! It's working well with your QueryStringAuthenticator, the ticket is well created ! Thanks again for your help :-) . I really appreciate it . I'm waiting for the new release of python-rtkit :-) .

Best regards, Lingjing

LingjingFrance commented 10 years ago

Hello mgaulton,

Your "Queue" in the debug message is empty , so you don't set the correct Queue information. In my case , the queue address is an e-mail address : "team.support@kelkoo.com" , and I set 'Queue': 'team.support' .

z4r commented 10 years ago

With python-rtkit 0.7.0 im considering this issue closed.

If you've any problem plz open another issue.