Advertisement
Guest User

Untitled

a guest
Jan 9th, 2018
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.51 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using Newtonsoft.Json.Linq;
  5.  
  6. namespace ApiSampleCode
  7. {
  8.    internal class GetMachine
  9.    {
  10.       private static void Main(string[] args)
  11.       {
  12.  
  13.          //GET/authentication/login
  14.  
  15.          /*The GetAuthorizationToken method makes a webrequest to the login endpoint, receiving a
  16.           * authorization token in Json format which is stored and parsed as a string in
  17.           * the userCode variable.*/
  18.          string GetAuthorizationToken()
  19.          {
  20.             var tokenWebRequest = (HttpWebRequest) WebRequest.Create(
  21.                "https://icebreaker-api.telliq.com/authentication/login?username={username}&password={password}");
  22.             //remember to change the {username} and {password}.
  23.             tokenWebRequest.Method = "GET";
  24.             tokenWebRequest.ContentType = "application/x-www-form-urlencoded";
  25.  
  26.             var loginResponse = (HttpWebResponse) tokenWebRequest.GetResponse();
  27.             var responseStream = new StreamReader(
  28.                loginResponse.GetResponseStream() ?? throw new InvalidOperationException(),
  29.                System.Text.Encoding.GetEncoding("utf-8"));
  30.             var authorizationToken = responseStream.ReadToEnd();
  31.             loginResponse.Close();
  32.  
  33.             var userCode = JObject.Parse(authorizationToken).Value<string>("userCode");
  34.  
  35.             return userCode;
  36.          }
  37.  
  38.          //GET/v1/machine
  39.          /*The code below call the get machine endpoint which require the userCode/token that we got when
  40.           * we called the login endpoint above. In the response we get all the machine Ids available for the logged in user which we write to the console.*/
  41.          var machineWebRequest = (HttpWebRequest) WebRequest.Create(
  42.             "https://icebreaker-api.telliq.com/v1/machine");
  43.          machineWebRequest.Method = "GET";
  44.          machineWebRequest.ContentType = "application/x-www-form-urlencoded";
  45.  
  46.          var usercode = GetAuthorizationToken();
  47.          machineWebRequest.Headers.Add("x-auth-token", usercode);
  48.  
  49.          var machineResponse = (HttpWebResponse) machineWebRequest.GetResponse();
  50.          var machineStreamReader = new StreamReader(
  51.             machineResponse.GetResponseStream() ?? throw new InvalidOperationException(),
  52.             System.Text.Encoding.GetEncoding("utf-8"));
  53.          var machines = machineStreamReader.ReadToEnd();
  54.          machineResponse.Close();
  55.  
  56.          Console.Write(machines);
  57.          Console.ReadLine();
  58.  
  59.  
  60.  
  61.  
  62.       }
  63.  
  64.    }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement