Python RallyDev:无法创建缺陷。服务器上说;无法分析输入…”;
原创帖子: 我试图以编程方式创建缺陷。我犯了几个错误,很难再进一步了。这里基本上是代码:Python RallyDev:无法创建缺陷。服务器上说;无法分析输入…”;,python,rally,Python,Rally,原创帖子: 我试图以编程方式创建缺陷。我犯了几个错误,很难再进一步了。这里基本上是代码: import requests, json rally_auth = ('my_user', 'my_pw') rally_auth_url = 'https://rally1.rallydev.com/slm/webservice/v2.0/security/authorize' rally_defect = 'https://rally1.rallydev.com/slm/webservice/v2.
import requests, json
rally_auth = ('my_user', 'my_pw')
rally_auth_url = 'https://rally1.rallydev.com/slm/webservice/v2.0/security/authorize'
rally_defect = 'https://rally1.rallydev.com/slm/webservice/v2.0/defect/defect'
workspace_ref = 'https://rally1.rallydev.com/slm/webservice/v2.0/workspace/12345'
fe_project_ref = 'https://rally1.rallydev.com/slm/webservice/v2.0/project/7890'
current_fe_release_ref = "https://rally1.rallydev.com/slm/webservice/v2.0/release/45678"
r = requests.get(rally_auth_url, auth=rally_auth)
token = r.json()['OperationResult']['SecurityToken']
url = rally_defect + '/create?key=' + token
payload = {
'Name': 'My defect',
'State': 'Open',
'Project': fe_project_ref,
'Rank': 120,
'Release': current_fe_release_ref,
'key': token
}
headers = {'content-type': 'application/json'}
r = requests.post(url, data=json.dumps(payload), auth=rally_auth, headers=headers)
您会注意到,我已经在帖子的URL和数据中添加了标记。API文档说我应该在URL中包含密钥,但是如果我没有将密钥包含在我获得的POST数据中:
{"CreateResult": {"_rallyAPIMajor": "2", "_rallyAPIMinor": "0", "Errors": ["Not authorized to perform action: Invalid key"], "Warnings": []}}
如果我包含了密钥,那么API请求的处理方式就不同了。它将在第一个逗号处失败
{"CreateResult": {"_rallyAPIMajor": "2", "_rallyAPIMinor": "0", "Errors": ["Cannot parse input stream due to I/O error as JSON document: Parse error: expected '}' but saw ',' [ chars read = >>>{\"Name\": \"My defect\",<<< ]"], "Warnings": []}}
如果您使用的是WS-API的v2.0,则更新和创建请求需要该令牌,因此将其包含在post请求url中是正确的 如果令牌没有附加到请求中,或者令牌对于特定会话无效,则会出现无效密钥错误。当直接命中端点时,我们必须使用cookie维护http会话,否则post将在新会话的上下文中发生-与我们获取令牌的会话不同 请看。它不是Python特有的,但在概念上是相同的 我注意到了有效载荷的等级。你有等级自定义字段吗?在Rally的v2.0中没有这样的内置字段。存在一个DragAndDropRank,它不是数字,通过提供120的值来设置它将不起作用。另外,您是否尝试在有效负载中使用双引号而不是单引号 有一个pyral-提供了方便的方法,因此您不必直接命中端点。目前它与WS-API的1.43兼容。该工具包没有得到官方支持,但我预计它将在2014年6月1.43不再受支持(per)之前进行更新,以使用WS-API的v2.0。 安全令牌是在v2.0中引入的。WS-API的1.43中不存在post请求的额外身份验证层,如果使用pyral,则不必处理令牌
import requests, json
rally_auth = ('my_user', 'my_pw')
rally_auth_url = 'https://rally1.rallydev.com/slm/webservice/v2.0/security/authorize'
rally_defect = 'https://rally1.rallydev.com/slm/webservice/v2.0/defect/defect'
workspace_ref = 'https://rally1.rallydev.com/slm/webservice/v2.0/workspace/12345'
fe_project_ref = 'https://rally1.rallydev.com/slm/webservice/v2.0/project/7890'
current_fe_release_ref = "https://rally1.rallydev.com/slm/webservice/v2.0/release/45678"
s = requests.Session()
r = s.get(rally_auth_url, auth=rally_auth)
token = r.json()['OperationResult']['SecurityToken']
url = rally_defect + '/create?key=' + token
payload = {
'Name': 'My defect',
'State': 'Open',
'Project': fe_project_ref,
'Rank': 120,
'Release': current_fe_release_ref,
}
headers = {'content-type': 'application/json'}
r = s.post(url, data=json.dumps(payload), headers=headers)