简体   繁体   English

使用python的HP QC REST API

[英]HP QC REST API using python

I tried to connect HP QC using python to create defects and attach files , but I am not able to connect with HP QC. 我尝试使用python连接HP QC来创建缺陷并附加文件 ,但我无法连接HP QC。 Here is my code: 这是我的代码:

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)

Now login works fine, but when try other API it asks for, I would get this message: 现在登录工作正常,但是当尝试其他API请求时,我会收到以下消息:

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

If the parameter has been added, then it goes back to login page. 如果已添加参数,则返回登录页面。

Seems that your urls are not well builded: 似乎您的网址没有很好地构建:

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

you will get: 你会得到:

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

qcbin twice qcbin两次

The way I do it is to based on python request Sessions. 我这样做的方法是基于python请求Sessions。 First I create a session, then post my credentials to ../authentication-point/alm-authenticate/ (or sth like this, you should check it) and then using this session I can get, post or do whatever I want. 首先我创建一个会话,然后将我的凭据发布到../authentication-point/alm-authenticate/ (或者像这样,你应该检查它)然后使用这个会话我可以得到,发布或做我想做的任何事情。

So: 所以:

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

I think it's a good url, but I can't check it right now :) 我认为这是一个很好的网址,但我现在无法检查:)

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

Just send a unicode string to your server and use the header for the basic Authorization as specified by the HP REST API: 只需将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)

POST by using the requests module in python is quite easy: 使用python中的请求模块进行POST非常简单:

requests.post(login_url, headers=header) 

That's it...now you are authenticated and you can proceed with next action :-) To check on that you can "GET" on: 就是这样......现在你已经过身份验证了,你可以继续进行下一步行动:-)要检查你是否可以“获取”:

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

you should get a code 200 --> That means you are authenticated. 你应该得到一个代码200 - >这意味着你是经过身份验证的。 Hope this help you. 希望这对你有所帮助。 Have a nice day and let me know if something is still not clear. 祝你有个愉快的一天,如果有什么不清楚,请告诉我。

ps: to send REST msg in python I am using requests module. ps:在python中发送REST消息我正在使用请求模块。 It is really easy! 真的很容易! You can create a session if you want to send multiple actions--> then work with that sessions--> ALM = requests.session(), then use ALM.post(whatever) and so on :-) 如果你想发送多个动作,你可以创建一个会话 - >然后使用那些会话 - > ALM = requests.session(),然后使用ALM.post(无论如何)等等:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM