我正在使用rest api和python自动化alm。我现在所能做的就是登录,登录和注销的logout.Only apis给出一个适当的response.All,其他的给出500/401错误。
import requests
from requests.auth import HTTPBasicAuth
from xml.etree.ElementTree import Element, SubElement, tostring, parse
ALM_USER_NAME = "XXXX"
ALM_PASSWORD = "XXXX"
ALM_DOMAIN = "XXXX"
ALM_PROJECT="XXXX"
ALM_URL ="http://xxxxxxxxx/qcbin/"
QC_LOGOUT_END_POINT = ALM_URL + "authentication-point/logout"
session = requests.Session()
session.verify = False
auth = session.post(ALM_URL + "authentication-point/authenticate",
auth=HTTPBasicAuth(ALM_USER_NAME, ALM_PASSWORD))
print("Authentication ", auth, auth.text, session.cookies)
site_session = session.post(ALM_URL + "rest/site-session")
print("Session ", site_session, site_session.text, session.cookies)
check = session.get(ALM_URL + "rest/is-authenticated")
print("Check ", check, check.text)
# Enforce JSON output
session.headers.update({ 'Accept': 'application/json','Cookie': check.headers['set-cookie']})
#projects = session.get(hpqc_server + "rest/domains/"+ALM_DOMAIN+"/projects")
# Post a New Defect
defect = dict()
defect['Type'] = 'defect'
defect['name'] = 'StackOverflow'
defect['user-10'] = 'Content' # User defined field
defect['severity'] = '1-Low'
defect['priority'] = '1-Low'
defect['detected-by'] = 'userid'
defect['creation-time'] = '2017-11-13'
# Call the generic method to build the xml data
defect_payload = generate_xml_data(defect)
deft=session.get(ALM_URL + "rest/domains/"+ALM_DOMAIN+"/projects/"+ALM_PROJECT+"/content/defects")
print(deft.status_code)
if session:
res = session.post(QC_LOGOUT_END_POINT)
if res.status_code == 200:
print ("ALM: Logged out")登录响应:200会话响应:201缺陷响应:401
发布于 2020-02-12 22:40:19
看起来您的请求缺少cookies。请调试/打印出所有cookie。在我的例子中(也使用python requests API和ALM 12.55),每个请求都会发送以下cookie(例如,读取缺陷):
ALM_USER
LWSSO_COOKIE_KEY
QCSession
XSRF-TOKEN
JSESSIONID所需步骤摘要:
authentication-point/authenticate发送身份验证请求。保存返回的LWSSO_COOKIE_KEY cookie。rest/site-session发送会话请求,并随LWSSO_COOKIE_KEY cookie一起发送。将返回的ALM_USER、QCSession、XSRF-TOKEN、JSESSIONID cookiesRequestsCookieJar.update(self, other))中。
在"# Enforce JSON output“行中,更新标题中的cookie。您设置了check的cookies。也许site_session的cookie不见了。
发布于 2020-02-12 23:12:09
我设法让它工作了。我需要对登录URL进行初始调用,以获取LWSSO_COOKIE_KEY和QCSession的cookies。在这之后,我得到了我需要的响应。
这是我的代码,由于安全原因,url和密码略有更改。此外,我通过查看Postman生成的python代码获得了加密的授权基本密码:
url_signin = "http://????????????/qcbin/api/authentication/sign-in"
url_defects = "http://??????????/qcbin/api/domains/{Domain name}/projects/{project_name}/defects"
payload = {}
headers1 = {
"Authorization": "Basic ?????????"}
si = requests.get(url_signin, headers = headers1)
QCsession = si.cookies.items()[2][1]
lwsso = si.cookies.items()[1][1]
headers = {
"Authorization": "Basic ??????????",
'Cookie': "QCSession="+ QCsession + "; "+"LWSSO_COOKIE_KEY=" +lwsso
}
response = requests.request("GET", url, headers=headers, data = payload)
print(response.text)如果你有任何问题,请告诉我
https://stackoverflow.com/questions/59838014
复制相似问题