0

如何为 rest api 调用传递 HP ALM 身份验证会话对象。我正在按照一些示例通过 REST API 连接到 HP ALM 以运行基本的 CRUD 操作。

示例之一 - https://github.com/vkosuri/py-hpalm/blob/master/hpalm/hpalm.py

下面是建立连接的代码片段,它工作得很好。我收到 200 OK 登录响应。

headers = {'Cookie' : lwssocookie}
headers["Accept"] = 'application/xml'
login_url = self.base_url + '/qcbin/authentication-point/authenticate'

resp = requests.get(login_url, headers=headers, auth=HTTPBasicAuth(self.username, self.password), verify=self.verify)
alm_session = resp.headers['Set-Cookie']
logger.debug("Is QC session launched: %s" %alm_session)
cookie = ";".join((lwssocookie, alm_session))

但是,即使我将 cookie 添加到标头,所有后续操作都因未经授权的错误而失败

self.headers['cookie'] = cookie
url = self.base_url + '/qcbin/rest/domains/' + self.domain + '/projects/' + self.project + '/test-instances'
response = requests.get(url, params=params, headers=self.getheaders())

谁能建议如何举行会议以运行操作以及我在这里缺少什么。

我还尝试在 get 调用中传递 cookie,如下所示,即使这样也没有用。

response = requests.get(url, params=params, headers=self.getheaders(), cookies=cookie)

先感谢您

4

1 回答 1

1

感谢requests.Session(),无需为 cookie 操作 HTTP 标头。

这是我为旧 ALM 版本 12.01 运行的代码片段,由于这篇文章,我花了几个小时来设计:

session = requests.Session()
session.verify = False

auth = session.post(hpqc_server + "authentication-point/authenticate?login-form-required=y",
                    auth=HTTPBasicAuth(username, password))
print("Authentication ", auth, auth.text, session.cookies)

site_session = session.post(hpqc_server + "rest/site-session")
print("Session ", site_session, site_session.text, session.cookies)

check = session.get(hpqc_server + "rest/is-authenticated")
print("Check ", check, check.text)

# Enforce JSON output
session.headers.update({ 'Accept': 'application/json' })
projects = session.get(hpqc_server + "rest/domains/DOMAIN/projects")
于 2018-09-21T07:35:50.453 回答