Example #1
0
        public ListofProjectsResponse.Projects PersonalAccessToken(string url, string personalAccessToken)
        {
            // encode our personal access token
            string credentials = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "", personalAccessToken)));

            // create a viewmodel that is a class that represents the returned json response
            ListofProjectsResponse.Projects viewModel = new ListofProjectsResponse.Projects();

            // use the httpclient
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(url);  // url of our account (https:// accountname.visualstudio.com)
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", credentials);

                // connect to the REST endpoint
                HttpResponseMessage response = client.GetAsync("_apis/projects?stateFilter=All&api-version=2.2").Result;

                // check to see if we have a succesfull respond
                if (response.IsSuccessStatusCode)
                {
                    // set the viewmodel from the content in the response
                    viewModel = response.Content.ReadAsAsync <ListofProjectsResponse.Projects>().Result;
                    // var value = response.Content.ReadAsStringAsync().Result;
                }

                viewModel.HttpStatusCode = response.StatusCode;

                return(viewModel);
            }
        }
Example #2
0
        public ListofProjectsResponse.Projects GetTeamProjectsByState()
        {
            // create a viewmodel that is a class that represents the returned json response
            ListofProjectsResponse.Projects viewModel = new ListofProjectsResponse.Projects();

            // use the httpclient
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(_configuration.UriString);
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", _credentials);

                // connect to the REST endpoint
                HttpResponseMessage response = client.GetAsync("_apis/projects?stateFilter=All&api-version=2.2").Result;

                // check to see if we have a succesfull respond
                if (response.IsSuccessStatusCode)
                {
                    // set the viewmodel from the content in the response
                    viewModel = response.Content.ReadAsAsync <ListofProjectsResponse.Projects>().Result;
                }

                viewModel.HttpStatusCode = response.StatusCode;

                return(viewModel);
            }
        }
Example #3
0
        private static ListofProjectsResponse.Projects ListProjects(string vstsAccountName, AuthenticationHeaderValue authHeader)
        {
            // create a viewmodel that is a class that represents the returned json response
            ListofProjectsResponse.Projects viewModel = new ListofProjectsResponse.Projects();

            // use the httpclient
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(String.Format("https://{0}.visualstudio.com", vstsAccountName));
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                client.DefaultRequestHeaders.Add("User-Agent", "VstsRestApiSamples");
                client.DefaultRequestHeaders.Add("X-TFS-FedAuthRedirect", "Suppress"); // Return the true HTTP Error rather than prompting for authentication
                client.DefaultRequestHeaders.Authorization = authHeader;

                // connect to the REST endpoint
                HttpResponseMessage response = client.GetAsync("_apis/projects?stateFilter=All&api-version=2.2").Result;

                // check to see if we have a succesfull respond
                if (response.IsSuccessStatusCode)
                {
                    viewModel = response.Content.ReadAsAsync <ListofProjectsResponse.Projects>().Result;
                    // var value = response.Content.ReadAsStringAsync().Result;
                }
                else if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
                {
                    // This often occurs if the token has expired.
                    // Acquire an updated token via the AuthenticationContext
                    throw new UnauthorizedAccessException();
                }
                else
                {
                    Console.WriteLine("{0}:{1}", response.StatusCode, response.ReasonPhrase);
                }

                viewModel.HttpStatusCode = response.StatusCode;

                return(viewModel);
            }
        }