【问题标题】:IBM RTC Python Create WorkitemIBM RTC Python 创建工作项
【发布时间】:2021-03-15 23:22:30
【问题描述】:

我目前正在寻找可以帮助使用 XML 有效负载创建工作项的 python 脚本。我尝试了 RTCClient 但它对未来没有太大帮助,因此我正在通过 Python 的 Requests 库寻找脚本

我尝试了 cURL 命令并且能够在 RTC 中创建工作项,但是当我尝试通过 Python 请求重复相同的操作时,我没有任何运气来实现它。下面是我用来实现相同目的的 sn-p。在我上一次 GET 期间,我收到 HTML 错误,因为“Javascript 被禁用或在您的浏览器中不可用”。我相信我的身份验证无法通过 Python 正常工作,而 cURL 也可以正常工作

谁能帮忙改正下面的语法

RTCCookieURL = 'https://clmtest:9443/jazz/authenticated/identity'
RTCGetCookie = requests.get(RTCCookieURL, verify=False)
RTCCookies=RTCGetCookie.cookies
print(RTCCookies)
RTCAuthURL = 'https://clmtest:9443/jazz/authenticated/j_security_check'
RTCHeaders = {
'Accept': 'text/xml',
'Content-Type': 'application/x-oslc-cm-change-request+xml'
}
RTCAuth = requests.get(RTCAuthURL, auth=HTTPBasicAuth('uname','pwd'), verify=False,      allow_redirects=True)
print(RTCAuth.cookies)
RTCGetCatalog = requests.get('https://clmtest:9443/jazz/oslc/workitems/catalog', verify=False,     cookies=RTCAuth.cookies)
print(RTCGetCatalog.content)

【问题讨论】:

  • 您尝试复制的 curl 命令是什么?请将它们编辑到您的问题中。

标签: python python-3.7 ibm-rational


【解决方案1】:

我猜您正在尝试复制我在某处看到的使用 Curl 分两步登录的示例 - 一个 GET 收集 cookie,然后一个 POST(因为 -d 数据包含在 Curl 命令中)进行表单身份验证,在 GET 上显式保存 cookie 并将这些 cookie 应用到后续命令。

您应该使用请求会话,因为它会为您完成所有 cookie 处理。

参考资料在这里,请参阅下面的标题 FORM 挑战 https://jazz.net/wiki/bin/view/Main/NativeClientAuthentication。如果处理得当,当发出需要登录的请求时,响应会指出这一点并告诉您去哪里进行身份验证,这比像您的代码那样简单地硬编码 URL 以及下面的简单示例是一个更好的计划。

注意事项:

  1. 您应该注意一点 - 身份验证在应用服务器上配置了超时,并且只要该超时未过期,执行显式登录仅适用于后续请求,然后您将面临挑战如果您忽略它,您将开始收到 403 响应。基本上,通常最好不要使用显式登录,而是始终尝试发出您想要发出的实际请求并检查响应标头(即使请求获得 200,例如登录前的 whoami,请参阅代码) 寻找身份验证的需要,然后进行登录,最后重放原始请求,由于需要身份验证,该请求不会产生任何影响。采取像这样处理每个请求的方法,然后通过重新身份验证自动处理身份验证到期。
  2. 下面的这段代码使用硬编码的 URL,所以在例如jts 上下文根从 /jts 更改。在更健壮的实现中,几乎没有硬编码的 url - 显然您的代码需要知道应用程序的 URL,例如https://myserver.com:9443/ccm1 然后我认为只需要最少的编码 - 例如 j_security_checkrootservices - 应该从根服务(项目目录)或响应中的内容/标题中找到所有其他 url。
  3. ccm 和 qm 自己进行身份验证,但 rm 委托给 jts 进行身份验证 - authrequired 的指示符告诉您去哪里,因此您不需要(不应该)对差异进行硬编码。
  4. 如参考资料中所述,您尝试直接登录时无法在 Tomcat 上运行,只能在 Liberty 上运行。另请参阅有关重放 Tomcat 原始请求的其他说明。

下面的这段代码复制了我对 Curl 登录序列的记忆,适用于使用 Liberty 用户注册表的 Form 登录 Liberty,这就是我对其进行测试的内容。 YMMV 与其他身份验证机制,这绝对不适用于 Jazz 授权服务器,它为登录执行不同的重定向。

import requests
import urllib3
    
# Disable the InsecureRequestWarning - pointless having warnings when
# accessing temporary servers which only ever have self-signed certs
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

SERVER = "https://clmtest:9443"

JTSCookieURL = SERVER+'/ccm/authenticated/identity'
JTSAuthURL = SERVER+'/ccm/j_security_check'
JTSWHOAMI = SERVER+"/ccm/whoami"
RootServices = SERVER+'/ccm/rootservices'

USERNAME='fred'
PWD = 'fred'

# get a session - use this for *every* request to the server
s = requests.session()

# when not authenticated, whoami returns 200 with HTML :-( - but it's a good test of authentication success
GetWhoAmI = s.get(JTSWHOAMI, verify=False)

print( f"{GetWhoAmI=}" )

# whoami when authenticated returns the user id - this is always on the jts    
# or when not authenticated returns html
if GetWhoAmI.content != SERVER+"/jts/users/"+USERNAME:
    print( "NOT AUTHENTICATED YET!!!")
else:
    raise Exception( "We are authenticated which should never happen before we login" )

#
# This works for (default) form authentication using Liberty user registry
#

# do a get to load up session with cookies
GetCookie = s.get(JTSCookieURL, verify=False)

print( f"{GetCookie=}" )

# do a post to authenticate
Auth = s.post(JTSAuthURL, data = { 'j_username': USERNAME, 'j_password': PWD}, verify=False,allow_redirects=True)

print(f"{Auth=}")

# now you can get protected resource - whoami 
GetWhoAmI = s.get(JTSWHOAMI, verify=False)

print( f"{GetWhoAmI=}" )

if GetWhoAmI.text != SERVER+"/jts/users/"+USERNAME:
#    print( "NOT AUTHENTICATED!!!")
    raise Exception( "NOT AUTHENTICATED" )
else:
    print( "HURRAY WE ARE AUTHENTICATED" )    

# now you can get protected resources, starting with rootservices

GetRootServices = s.get(RootServices, verify=False)

print( f"{GetRootServices=}" )

#print(GetRootServices.content)

【讨论】:

  • 谢谢@barny ... 像魅力一样工作
  • 好 - 这是惯例,但完全是可选的,如果我的回答回答了你的问题,那么你会接受这个答案,也许还会支持我的回答。
【解决方案2】:

/rm 的 curl 等效项(对于 Windows,对于其他操作系统,只需删除 rem/echo 行)类似于(对于 https://SERVER:9443 和管理员用户/密码 myid/mypassword):

rem GET to receive the JazzFormAuth cookie
rem important to use the -L option so the authentication redirects are followed
curl -k -L -c cookies.txt "https://SERVER:9443/jts/authenticated/identity" -v
echo ******************************************************************************************

rem POST to authenticate
rem important to use the -L option so the authentication redirects are followed
curl -k -L -b cookies.txt -c cookies.txt -d "" "https://SERVER:9443/jts/j_security_check?j_username=myid&j_password=mypassword" -v

rem GET a protected resource - if fails, returns 302 (because -L isn't specified on this request, if it fails then redirect to auth isn't followed)
curl -k -b cookies.txt -c cookies.txt "https://SERVER:9443/rm/oslc_rm/catalog" -v -H "OSLC-Core-Version: 2.0" -H "Accept: application/rdf+xml"

然后对于进一步的请求,请始终使用-b cookies.txt -c cookies.txt 选项来提供会话身份验证。注意 cookie 仅在身份验证超时到期之前有效 - 可能是六个小时。

此示例也适用于 ccm(Engineering Workflow Manager,EWM,以前称为 RTC)以及 DOORS Next,并且应该适用于 Engineering Test Manager(ETM,以前称为 RQM)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多