Sample code to consume the Device API using C#.

using Newtonsoft.Json.Linq;using System;using System.IO;
using System.Net;
using System.Security.Cryptography;
using System.Text;

string API_KEY = "YOUR API KEY";
string SECRET = "YOUR SECRET KEY";

private void Main()
{
    //GET ALL DEVICES
    GetDevice(null);
    //GET DEVICE BY ID            
    GetDevice(1);
}

private void GetDevice(int? deviceID)        
{
    string apiHost = "https://www.kbremote.net"; //API HOST
    string apiPath = "api"; //PATH TO API
    string apiFunction = "device"; //API FUNCTION NAME
    string url = string.Join("/", apiHost, apiPath, apiFunction);
    if (deviceID != null)
        url = string.Join("/", url, deviceID.ToString());
        
    string timeStamp = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss");
    
    //CREATE REQUEST TO SERVER
    WebRequest request = WebRequest.Create(url);

    //SET REQUEST TYPE
    request.Method = "GET";
    
    //CREATE MESSAGE IN STANDARD FORMAT TO HASH
    string message = string.Join("/", request.Method + timeStamp, apiPath, apiFunction);
    if (deviceID != null)
        message = string.Join("/", message, deviceID.ToString());
        
    //HASH THE MESSAGE
    string messageHash = ComputeHash(SECRET, message);
    request.Headers.Add("Authentication", API_KEY + ":" + messageHash);
    request.Headers.Add("Timestamp", timeStamp);
    try
    {
        //EXECUTE REQUEST AND GET RESPONSE
        WebResponse response = request.GetResponse();

        //GET THE RESPONSE STREAM TO STRING
        Stream stream = response.GetResponseStream();
        StreamReader reader = new StreamReader(stream);
        String jsonString = reader.ReadToEnd();
        reader.Close();
        
        //PARSE RESPONSE STRING INTO JSON.NET ARRAY                
        if (deviceID == null)
        {
            JArray json = JArray.Parse(jsonString);
            Console.WriteLine(json.ToString());
        }
        else //USE JOBJECT IF ONLY EXPECTING SINGLE DEVICE  
        {
            JObject json = JObject.Parse(jsonString);
            Console.WriteLine(json.ToString());
        }
    }
    catch (WebException ex)
    {
        if (ex.Response != null) //HANDLE HTTP ERROR CODE
        {
            HttpWebResponse httpResponse = (HttpWebResponse)ex.Response;
            Console.WriteLine("Http status code: " + httpResponse.StatusCode);
        }
        else //HANDLE SOMETHING MORE SERIOUS SUCH AS NETWORK CONNECTION NOT AVAILABLE
        {
            Console.WriteLine(ex.Message);
        }
    }
}

private static string ComputeHash(string secret, string message)
{
    var key = Encoding.UTF8.GetBytes(secret.ToUpper());
    string hashString;
    using (var hmac = new HMACSHA256(key))
    {
        var hash = hmac.ComputeHash(Encoding.UTF8.GetBytes(message));
        hashString = Convert.ToBase64String(hash);
    }
    return hashString;
}

This article was helpful for 3 people. Is this article helpful for you?