簡體   English   中英

無法使用 rest api 和 python 獲取並發布到 alm。 總是收到 401 狀態碼

[英]Not able to get and post to alm using rest api and python. Always getting 401 status code

我正在使用 rest api 和 python 自動化 alm。 我現在所能做的就是登錄和注銷。只有用於登錄和注銷的 api 會給出正確的響應。所有其他都給出 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

看起來您的請求缺少 cookie。 請調試它/打印出所有 cookie。 在我的例子中(也在 ALM 12.55 中使用 python 請求 API)每個請求都會發送以下 cookie(例如讀取缺陷):

ALM_USER
LWSSO_COOKIE_KEY
QCSession
XSRF-TOKEN
JSESSIONID

所需步驟摘要:

  1. 將身份驗證請求發布到authentication-point/authenticate 保存返回的LWSSO_COOKIE_KEY cookie。
  2. 將會話請求發布到rest/site-session並發送LWSSO_COOKIE_KEY cookie。 保存返回的ALM_USERQCSessionXSRF-TOKENJSESSIONID cookie
  3. 將所有 cookie 合並到一個 cookie 容器中( RequestsCookieJar.update(self, other)

在您的“# Enforce JSON 輸出”行中,您更新標頭中的 cookie。 您設置了check的 cookie。 也許site_session的 cookie 丟失了。

我設法讓它工作。 我需要對登錄 URL 進行初始調用以獲取 LWSSO_COOKIE_KEY 和 QCSession 的 cookie。 在此之后,我得到了我需要的回應。

這是我的代碼,出於安全原因,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)

如果您有任何問題,請告訴我

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM