Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Net;
- using Newtonsoft.Json.Linq;
- namespace ApiSampleCode
- {
- internal class GetMachine
- {
- private static void Main(string[] args)
- {
- //GET/authentication/login
- /*The GetAuthorizationToken method makes a webrequest to the login endpoint, receiving a
- * authorization token in Json format which is stored and parsed as a string in
- * the userCode variable.*/
- string GetAuthorizationToken()
- {
- var tokenWebRequest = (HttpWebRequest) WebRequest.Create(
- "https://icebreaker-api.telliq.com/authentication/login?username={username}&password={password}");
- //remember to change the {username} and {password}.
- tokenWebRequest.Method = "GET";
- tokenWebRequest.ContentType = "application/x-www-form-urlencoded";
- var loginResponse = (HttpWebResponse) tokenWebRequest.GetResponse();
- var responseStream = new StreamReader(
- loginResponse.GetResponseStream() ?? throw new InvalidOperationException(),
- System.Text.Encoding.GetEncoding("utf-8"));
- var authorizationToken = responseStream.ReadToEnd();
- loginResponse.Close();
- var userCode = JObject.Parse(authorizationToken).Value<string>("userCode");
- return userCode;
- }
- //GET/v1/machine
- /*The code below call the get machine endpoint which require the userCode/token that we got when
- * 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.*/
- var machineWebRequest = (HttpWebRequest) WebRequest.Create(
- "https://icebreaker-api.telliq.com/v1/machine");
- machineWebRequest.Method = "GET";
- machineWebRequest.ContentType = "application/x-www-form-urlencoded";
- var usercode = GetAuthorizationToken();
- machineWebRequest.Headers.Add("x-auth-token", usercode);
- var machineResponse = (HttpWebResponse) machineWebRequest.GetResponse();
- var machineStreamReader = new StreamReader(
- machineResponse.GetResponseStream() ?? throw new InvalidOperationException(),
- System.Text.Encoding.GetEncoding("utf-8"));
- var machines = machineStreamReader.ReadToEnd();
- machineResponse.Close();
- Console.Write(machines);
- Console.ReadLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement