【问题标题】:HP QC REST API using python使用 python 的 HP QC REST API
【发布时间】:2017-01-31 07:50:21
【问题描述】:

我尝试使用 python 连接 HP QC 以创建缺陷并附加文件,但我无法连接 HP QC。这是我的代码:

domain='DEFAULT_773497139'
project='773497139_DEMO'
import requests

url = "https://almalm1250saastrial.saas.hpe.com/qcbin/"

querystring = {"username":"user@gmail.com","password":"password"}

headers = {
    'cache-control': "no-cache",
    'token': "5d33d0b7-1d04-4989-3349-3005b847ab7f"
    }

response = requests.request("POST", url, headers=headers, params=querystring)

#~ print(response.text)

print response.headers
new_header = response.headers
new_url = url+ u'rest/domains/'+domain+u'/projects/'+project
new_querystring = {
"username":"user@gmail.com",
"password":"password",
"domain":'DEFAULT_773497139',
"project":'773497139_DEMO'
    }
print new_url
response = requests.request("POST", new_url, headers=new_header, params=new_querystring)
print(response.text)

现在登录工作正常,但是当尝试它要求的其他 API 时,我会收到以下消息:

Authentication failed. Browser based integrations - to login append '?login-form-required=y' to the url you tried to access

如果已添加参数,则返回登录页面。

【问题讨论】:

  • 反应是什么?
  • 响应 202 但它转到登录页面 @DanielSanchez
  • 对于域 api,登录后响应为 405,它要求进行身份验证
  • 代码已更新@DanielSanchez

标签: python rest api post hp-quality-center


【解决方案1】:

似乎您的网址构建得不好:

base_url ='https://server.saas.hpe.com/qcbin/'
base_url + '/qcbin/rest/domains/

你会得到:

..../qcbin/qcbin/...

qcbin 两次

【讨论】:

  • 更改 url 后相同的响应 405
【解决方案2】:

我这样做的方式是基于python request Sessions。首先我创建一个会话,然后将我的凭据发布到../authentication-point/alm-authenticate/(或者像这样,你应该检查它),然后使用这个会话我可以获取、发布或做任何我想做的事情。

所以:

s = requests.Session()
s.post(`../authentication-point/alm-authenticate/`, data=credentials)
# now session object is authenticated and recognized
# you can s.post, s.get or whatever

我认为这是一个很好的网址,但我现在无法检查:)

【讨论】:

  • 如何传递数据参数(示例)它抛出我无法找到外部认证的用户
  • 所以首先你有querystring ={"username":"user@gmail.com","password":"password"}然后你做session.post(url+'authentication_point/authenticate', data=querystring)现在如果你想在new_url上发布一些new_querystring你做session.post(new_url, data=new_querystring)。它应该可以工作。
【解决方案3】:

LWSSO cookie (LWSSO_COOKIE_KEY) 解决了会话问题。

【讨论】:

    【解决方案4】:

    只需向您的服务器发送一个 unicode 字符串,并使用 HP REST API 指定的基本授权标头:

    login_url = u'https://almalm1250saastrial.saas.hpe.com/qcbin/authentication-point/authenticate'
    username,password = user,passwd
    logs  = base64.b64encode("{0}:{1}".format(username, password))
    header['Authorization'] = "Basic {}".format(logs)
    

    在 python 中使用 requests 模块进行 POST 非常简单:

    requests.post(login_url, headers=header) 
    

    就是这样......现在您已通过身份验证,您可以继续下一步操作 :-) 要检查您是否可以“获取”:

    login_auth = u'https://almalm1250saastrial.saas.hpe.com/qcbin/rest/is-authenticated
    

    您应该得到一个代码 200 --> 这意味着您已通过身份验证。 希望这对您有所帮助。祝您有美好的一天,如果还有什么不清楚的地方,请告诉我。

    p.s.:在 python 中发送 REST msg 我正在使用 requests 模块。这真的很容易!如果您想发送多个操作,您可以创建一个会话--> 然后使用该会话--> ALM = requests.session(),然后使用 ALM.post(whatever) 等等 :-)

    【讨论】:

      猜你喜欢
      • 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
      相关资源
      最近更新 更多