VersionOne:query.v1如何为client.Headers [“Cookie”]生成.V1.Ticket.VersionOne.Web票证

时间:2013-09-06 21:18:23

标签: c# versionone

我可以访问Versionone的Summer2013。 我试图通过新的query.v1使用Json示例访问它,该示例使用如下所示的客户端头cookie:

_client.Headers["Cookie"] = ".V1.Ticket.VersionOne.Web=" + ticket;

如何为Cookie生成.V1.Ticket.VersionOne.Web故障单?

Json示例:https://github.com/versionone/versionone-oauth2-examples/blob/master/csharp/YamlClient/Program.cs

注意:我已通过这些说明生成了OAuth 2令牌,但它不包含票证。 https://community.versionone.com/Developers/Developer-Library/Documentation/API/Security/Oauth_2.0_Authentication/Using_OAuth_2.0_for_Web_Server_Applications

以下是代码:

namespace V1Json
{
    class JsonClient
    {
        private readonly Uri _url;
        private readonly string _ticket;
        private WebClient _client;

        public JsonClient(string url, string ticket)
        {
            _url = new Uri(url);
            _ticket = ticket;
            _client = new WebClient { Encoding = Encoding.UTF8 };
           _client.Headers["Cookie"] = ".V1.Ticket.VersionOne.Web=" + ticket;
        }

        public List<List<dynamic>> GetResultSets(string querybody)
        {
            var resultbody = _client.UploadString(_url, "SEARCH", querybody);
            return JsonConvert.DeserializeObject<List<List<dynamic>>>(resultbody);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var url = "https://versionone-test.acme.com/summer13_demo/query.v1";
             var authTicket = "AAEAAGvqd3ylmW0FphkxxxHASSMoCrEa...";

            var client = new JsonClient(url, authTicket);

1 个答案:

答案 0 :(得分:0)

我刚刚为您更新了该示例,包括使其与Joe Koberg最近创建的新OAuth2Client代码一起使用。

新版本的示例包括stored_credentials.json和client_secrets.json文件,这些文件已经设置为在http://www14.v1host.com/v1sdktesting处对我们的公共测试服务器起作用。要查看项目或修改部分数据,请使用 admin / admin 登录。

它包含一个简单的JsonClient,它利用Newtonsoft来解析从服务器返回的JSON。

它仍位于https://github.com/versionone/versionone-oauth2-examples/blob/master/csharp/YamlClient/Program.cs

使用JsonClient示例如下所示:

IStorage credentials = new Storage.JsonFileStorage(
            "../../client_secrets.json", "../../stored_credentials.json");
        const string scopes = "query-api-1.0 apiv1";
        const string url = "https://www14.v1host.com/v1sdktesting/query.v1";

var client = new JsonClient(credentials, url, scopes);

const string queryBody = @"
from: Scope
select:
    - Name
    - Workitems.@Count
    - Workitems:PrimaryWorkitem.@Count
    - Workitems:PrimaryWorkitem[Estimate>'0'].@Count
    - Workitems:PrimaryWorkitem[Estimate='0'].@Count
    - Workitems:PrimaryWorkitem[Estimate>'0'].Estimate.@Sum
    - from: Workitems:PrimaryWorkitem[Estimate>'0']
      select:
        - Name
        - Estimate
";
        var resultSets = client.GetResultSets(queryBody).ToArray();

        foreach (var result in resultSets[0]) // Rember that query.v1 returns a resultSet of resultSets!
        {
            Console.WriteLine(result["Name"]);
            Console.WriteLine("Total # of workitems: " + result["Workitems.@Count"]);
            Console.WriteLine("Total # of Primary workitems: " + result["Workitems:PrimaryWorkitem.@Count"]);
            Console.WriteLine("Total # of Estimated Primary workitems: " +
                              result["Workitems:PrimaryWorkitem[Estimate>'0'].@Count"]);
            Console.WriteLine("Total # of Unestimated Primary workitems: " +
                              result["Workitems:PrimaryWorkitem[Estimate='0'].@Count"]);
            Console.WriteLine("Sum of all Estimated Primary workitems: " +
                              result["Workitems:PrimaryWorkitem[Estimate>'0'].Estimate.@Sum"]);
            foreach (var estimatedWorkitem in result["Workitems:PrimaryWorkitem[Estimate>'0']"])
            {
                Console.WriteLine(estimatedWorkitem["Name"] + " : " + estimatedWorkitem["Estimate"]);
            }
            Console.WriteLine("\n");
        }

        Console.Write("Press any key to exit...");
        Console.ReadLine();
    }
}