using System.Globalization;
using System.Security.Cryptography;
public static void Main()
string PATH = "/v1/ping";
string response = doRequest("GET", PATH);
Console.WriteLine(response);
private static string doRequest(string httpMethod, string path, string contentType = null)
string contentMD5 = null;
string responseText = null;
DateTime requestDate = DateTime.UtcNow;
string authenticationUrl = "https://api.ninjarmm.com" + path;
string stringToSign = getStringToSign(httpMethod, contentMD5, contentType, requestDate, path);
Console.WriteLine("stringToSign : " + stringToSign);
string signature = getSignature("vcbifeflj2q6hjf4uaj2eepk6dr3drbpm1ems3bs", stringToSign);
Console.WriteLine("signature : " + signature);
string authorizationHeaderValue = "NJ " + "2DGLUS2Q0BM18O728B5M" + ":" + signature;
Console.WriteLine("authorizationHeaderValue : " + authorizationHeaderValue);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(authenticationUrl);
request.Method = httpMethod;
request.ContentType = contentType;
request.Headers.Add("Authorization", authorizationHeaderValue);
request.Headers.Add("x-nj-date", RFC1123_DATE_TIME_FORMATTER(requestDate));
request.ProtocolVersion = HttpVersion.Version11;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (var reader = new System.IO.StreamReader(response.GetResponseStream()))
responseText = reader.ReadToEnd();
private static string RFC1123_DATE_TIME_FORMATTER(DateTime requestDate)
var usCulture = new CultureInfo("en-US");
var str = requestDate.ToString("ddd, dd MMM yyyy HH:mm:ss \'GMT\'", usCulture);
private static String getStringToSign(String httpMethod, String contentMD5, String contentType, DateTime requestDate, String canonicalPath)
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append((httpMethod + "\n"));
stringBuilder.Append((contentMD5 != null ? contentMD5 + "\n" : "\n"));
stringBuilder.Append((contentType != null ? contentType + "\n" : "\n"));
stringBuilder.Append(RFC1123_DATE_TIME_FORMATTER(requestDate) + "\n");
stringBuilder.Append(canonicalPath);
return stringBuilder.ToString();
private static String getSignature(String secretAccessKey, String stringToSign)
var stringToSignBytes = System.Text.Encoding.UTF8.GetBytes(stringToSign);
string encodedString = System.Convert.ToBase64String(stringToSignBytes).Replace("\n", "").Replace("\r", "");
HMACSHA1 hmac = new HMACSHA1(enc.GetBytes(secretAccessKey));
byte[] hmacBytes = hmac.ComputeHash(enc.GetBytes(encodedString));
var signature = System.Convert.ToBase64String(hmacBytes).Replace("\n", "");