Skip to main content
 首页 » 编程设计

python之如何使用 python-rtkit 在 rt 中创建票证

2025年05月04日11mq0036

我在使用一个简单的脚本时遇到了问题,该脚本的唯一目的是创建一张票,每次我得到

401 Credentials Required 

我从 https://github.com/z4r/python-rtkit 中逐字记录了我的代码. (出于安全原因,对 URL、用户名和密码进行了编辑)

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') 
 
resource = RTResource('http://rt.example.com/REST/1.0/', 'user', 'pass', BasicAuthenticator) 
 
#create a ticket 
content = { 
    'content': { 
        'Queue': 'General - unassigned', #General - unassigned is the name of the desired queue 
        'Subject' : 'Test Ticket 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) 

这是我从这里得到的输出

[DEBUG] POST ticket/new 
[DEBUG] {'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8', 'Accept': 'text/plain'} 
[DEBUG] 'content=Queue: General - unassigned\nSubject: Test Ticket Python' 
[INFO] POST 
[INFO] http://rt.example.com/REST/1.0/ticket/new 
[DEBUG] HTTP_STATUS: 200 OK 
[DEBUG] 'RT/3.6.6 401 Credentials Required\n' 
[DEBUG] RESOURCE_STATUS: 401 Credentials required 
[INFO] []   
[INFO] [] 

有人知道怎么解决吗?我只需要能够用 python 制作门票。因此,只要达到最终目标,其他不使用 python-rtkit 的方法也可以。

编辑: 从我的浏览器(登录后)打开 url 会产生

RT/3.6.6 200 Ok 
 
# Invalid object specificaiton: 'index.html' 
 
id: index.html 

从我的浏览器(登录前)打开 url 会产生

RT/3.6.6 401 Credentials Required 

我对任何互联网都不是特别了解,所以我不确定如何查看返回的服务器 header 。值得注意的是,我已经尝试过 Cookie 身份验证,但没有任何更好的结果。

编辑: @Zaroth 的请求。我正在使用 https。

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> 
<html><head> 
<title>302 Found</title> 
</head><body> 
<h1>Found</h1> 
<p>The document has moved <a href="https://rt.example.com/REST/1.0/">here</a>.</p> 
<hr> 
<address>Apache/2.2.3 (Red Hat) Server at rt.hdms.com Port 80</address> 
</body></html> 

请您参考如下方法:

好吧,我不知道为什么它第一次不工作,但我现在通过使用 CookieAuthentication 而不是 BasicAuthentication 设法让它工作。