使用Ruby rally_api更新自定义字段的值时出现无效的键错误

时间:2013-09-20 20:50:29

标签: ruby rally

当我尝试更新这样的自定义字段时:

@rally.update('defect', 1234567890, {'c_CustomField'=>newValue})

我明白了:

Error on request - https://rally1.rallydev.com/slm/webservice/v2.0/defect/1234567890.js - {:errors=>["Not authorized to perform action: Invalid key"]}

1 个答案:

答案 0 :(得分:1)

您收到的错误特定于WS API的v2.0。请参阅WS API documentation中的授权部分。在v2.0中,需要安全令牌来授权创建和更新请求。

例如,如果使用更新或创建URL更新或创建工件,则必须首先获取安全令牌:

使用此端点获取安全令牌:

`https://rally1.rallydev.com/slm/webservice/v2.0/security/authorize`

响应:

{"OperationResult": {"_rallyAPIMajor": "2", "_rallyAPIMinor": "0", "Errors": [], "Warnings": [], "SecurityToken": "6a4b8....."}}

POST

`https://rally1.rallydev.com/slm/webservice/v2.0/HierarchicalRequirement/create?key=6a4b8...`.

然而,rally_api gem 0.9.20使其透明,并且无需明确请求令牌。

以下是自定义字段更新的示例:

require 'rally_api'

#Setup custom app information
headers = RallyAPI::CustomHttpHeader.new()
headers.name = "edit custom field"
headers.vendor = "Nick M RallyLab"
headers.version = "1.0"

# Connection to Rally
config = {:base_url => "https://rally1.rallydev.com/slm"}
config[:username] = "user"
config[:password] = "secret"
config[:workspace] = "W"
config[:project] = "P"
config[:version] = "v2.0"
config[:headers] = headers #from RallyAPI::CustomHttpHeader.new()

@rally = RallyAPI::RallyRestJson.new(config)

query = RallyAPI::RallyQuery.new()
query.type = :defect
query.fetch = "Name,FormattedID,CreationDate,Owner,UserName,c_MyCustomField"
query.workspace = {"_ref" => "https://rally1.rallydev.com/slm/webservice/v2.0/workspace/1111.js" } 
query.project = {"_ref" => "https://rally1.rallydev.com/slm/webservice/v2.0/project/2222.js" }
query.page_size = 200 #optional - default is 200
query.limit = 1000 #optional - default is 99999
query.project_scope_up = false
query.project_scope_down = true
query.order = "Name Asc"
query.query_string = "(FormattedID = DE13)"

results = @rally.find(query)

results.each do |d|
    puts "MyCustomField: #{d["c_MyCustomField"]}"
    d.read
    field_updates = {"c_MyCustomField" => "new text goes here"}
    d.update(field_updates)
    puts "MyCustomField: #{d["c_MyCustomField"]}"
end