VersionOne: query.v1 how to generate a .V1.Ticket.VersionOne.Web ticket for client.Headers["Cookie"]

523 Views Asked by At

I have access to Summer2013 of Versionone. I am trying to access it via new query.v1 using the Json Example which uses a client header cookie that looks like this:

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

How do I generate a .V1.Ticket.VersionOne.Web ticket for the cookie?

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

Note: I have generated a OAuth 2 token via these directions but it doesn't contain a ticket. https://community.versionone.com/Developers/Developer-Library/Documentation/API/Security/Oauth_2.0_Authentication/Using_OAuth_2.0_for_Web_Server_Applications

Here's the code:

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

There are 1 best solutions below

2
On

I've just updated that example for you, including making it work with the new OAuth2Client code that Joe Koberg created recently.

The new version of the sample includes stored_credentials.json and client_secrets.json files that have already been set up to work against our public test server at http://www14.v1host.com/v1sdktesting. To see the projects there or modify some of the data, use admin / admin to login.

It includes a simple JsonClient which utilizes Newtonsoft to parse the JSON returned from the server.

It's still located at https://github.com/versionone/versionone-oauth2-examples/blob/master/csharp/YamlClient/Program.cs

Using the JsonClient example looks like this:

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();
    }
}