3

在浏览器的 REST 客户端中,当我发布到此 URL 时

https://rally1.rallydev.com/slm/webservice/v2.0/HierarchicalRequirement/create?key=123abc

使用此请求正文:

{
"HierarchicalRequirement": 
{"Name": "mystory"}
}

它有效,但是

curl -u 'user@co.com:password' -H "Content-Type: application/json" -d  '{"HierarchicalRequirement": {"Name": "mystory"}}' https://rally1.rallydev.com/slm/webservice/v2.0/HierarchicalRequirement/create?key=123abc

产生无效的密钥错误:

{"CreateResult": {"_rallyAPIMajor": "2", "_rallyAPIMinor": "0", "Errors": ["Not authorized to perform action: Invalid key"], "Warnings": []}}

我最初认为安全密钥过期(是否曾经?)但在 REST 客户端中再次成功地重用了它。

4

1 回答 1

2

只要 HTTP 会话有效,授权密钥就有效。浏览器 REST 客户端和命令行 curl 之间的区别在于浏览器通过会话 cookie 自动维护 HTTP 会话。如果没有会话 cookie,每个 curl 请求都会构成一个新的 HTTP 会话。

您可以告诉 curl 存储会话 cookie 以保持 HTTP 会话。就是这样:

$ curl -u "user@company.com:topsecret" https://rally1.rallydev.com/slm/webservice/v2.0/security/authorize -c cookie.txt

回复:

{ "OperationResult" : { "Errors" : [  ],
      "SecurityToken" : "fb34ea43-21b9-314f-e23d-1c8ad281b42b",
      "Warnings" : [  ],
      "_rallyAPIMajor" : "2",
      "_rallyAPIMinor" : "0"
    } }

注意cookie中的会话ID信息:

$ more cookie.txt

# Netscape HTTP Cookie File
# http://curl.haxx.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.

#HttpOnly_rally1.rallydev.com   FALSE   /       TRUE    0       JSESSIONID      qs-app-tutewruthe4p3favagatR4qakat.qs-app-02
#HttpOnly_.rally1.rallydev.com  TRUE    /       TRUE    0       ZSESSIONID      thUZExetAxAj6JaquStaZaPresPe8He3aPhawAb2pU
#HttpOnly_.rally1.rallydev.com  TRUE    /       TRUE    0       SUBBUCKETID     4343

最后,使用通过授权端点获得的令牌和-bcurl 的标志来指定 cookie 文件,使用 curl 发出 create 命令:

$ curl -u 'user@company.com:topsecret' -H "Content-Type: application/json" -d '{"HierarchicalRequirement": {"Name": "mystory"}}' https://rally1.rallydev.com/slm/webservice/v2.0/HierarchicalRequirement/create?key=fb34ea43-21b9-314f-e23d-1c8ad281b42b -b cookie.txt

回复:

{ "CreateResult" : { "Errors" : [  ],
      "Object" : {
          "CreationDate" : "2013-07-06T15:00:32.380Z",
          "LastUpdateDate" : "2013-07-06T15:00:32.437Z",
          "Name" : "mystory",
          "ObjectID" : 12345678920,
          "Project" : { "_rallyAPIMajor" : "2",
              "_rallyAPIMinor" : "0",
              "_ref" : "https://rally1.rallydev.com/slm/webservice/v2.0/project/12345678911",
              "_refObjectName" : "My Project",
              "_type" : "Project"
            },
          "Rank" : 10433947185.0,
          "Subscription" : { "_rallyAPIMajor" : "2",
              "_rallyAPIMinor" : "0",
              "_ref" : "https://rally1.rallydev.com/slm/webservice/v2.0/subscription/12345678912",
              "_refObjectName" : "My Subscription",
              "_type" : "Subscription"
            },
          "Workspace" : { "_rallyAPIMajor" : "2",
              "_rallyAPIMinor" : "0",
              "_ref" : "https://rally1.rallydev.com/slm/webservice/v2.0/workspace/12345678913",
              "_refObjectName" : "My Workspace",
              "_type" : "Workspace"
            },
          "_CreatedAt" : "just now",
        },
      "Warnings" : [  ],
      "_rallyAPIMajor" : "2",
      "_rallyAPIMinor" : "0"
    } }
于 2013-07-06T15:04:19.637 回答