C#

using System;
using System.Security.Cryptography;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Web;

public class Program
{
    static String entrypoint = "http://www.lingkapis.com";
    static String apikey = "[your key]";
    static String secret = "[your secret]";
    private static readonly Encoding encoding = Encoding.UTF8;

    public static void Main()
    {
        Program.servicecall("/v1/@self/ps/courses", "GET");
        Console.Read();
    }

    public static async void servicecall(String service, String metho)
    {
        String requestDate;
        UriBuilder uri = new UriBuilder(entrypoint + service);
        Console.WriteLine(uri);

        requestDate = DateTime.UtcNow.ToString("R");

        HttpClientHandler handler = new HttpClientHandler() { };
        string fullMessage = "date: " + requestDate + "\n(request-target): " + method.toLower() + " " + service;
        using (var client = new HttpClient(handler))
        {

            string hashString = CreateSignature(fullMessage, secret);
            String authHeader = "Signature keyId=\"" + apikey + "\",headers=\"date (request-target)\",algorithm=\"hmac-sha1\",signature=\"" + WebUtility.UrlEncode(hashString) + "\"";

            client.BaseAddress = new System.Uri(entrypoint);
            client.DefaultRequestHeaders.Add("Date", requestDate);
            client.DefaultRequestHeaders.Add("Authorization", authHeader);
            var resp2 = await client.GetAsync(service);
            var aaa = resp2.Content;
            string result = await aaa.ReadAsStringAsync();
            Console.WriteLine(result);

        }
    }

    private static string CreateSignature(string message, string secret)
    {
        secret = secret ?? "";
        var encoding = new System.Text.ASCIIEncoding();
        byte[] keyByte = encoding.GetBytes(secret);
        byte[] messageBytes = encoding.GetBytes(message);
        using (var hmacsha1 = new HMACSHA1(keyByte))
        {
            byte[] hashmessage = hmacsha1.ComputeHash(messageBytes);
            return Convert.ToBase64String(hashmessage);
        }
    }

}

Last updated