Last active
August 2, 2021 07:19
-
-
Save tomas-rampas/3347c83ffe2893088e567e94d5d1114c to your computer and use it in GitHub Desktop.
Shrimpy.io Signing Request Sample in C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Net.Http; | |
using System.Security.Cryptography; | |
using System.Text; | |
/* | |
* This gist shows a sample of signing a GET request for shrimpy.io | |
* based on Pyton sample on https://developers.shrimpy.io/docs/?python#creating-a-request | |
*/ | |
namespace shrimpy_test | |
{ | |
class Program | |
{ | |
static string apiKey = "YOUR-API-KEY"; | |
static string pk = "YOUR-PRIVATE-KEY"; | |
private static readonly HttpClient client = new HttpClient(); | |
static void Main(string[] args) | |
{ | |
string secretKey = pk; | |
TimeSpan t = (DateTime.UtcNow - new DateTime(1970, 1, 1)); | |
long nonce = (long)t.TotalMilliseconds; | |
string baseUrl = "https://dev-api.shrimpy.io"; | |
string requestPath = "/v1/users"; | |
string body = ""; | |
string method = "GET"; | |
string prehashString = string.Join("", requestPath, method, nonce.ToString(), body); | |
string signature_b64 = GetSignature(secretKey, prehashString); | |
HttpRequestMessage msg = new HttpRequestMessage(HttpMethod.Get, $"{baseUrl}{requestPath}"); | |
msg.Content = new StringContent(body, Encoding.UTF8, "application/json"); | |
client.DefaultRequestHeaders.Add("DEV-SHRIMPY-API-KEY", apiKey); | |
client.DefaultRequestHeaders.Add("DEV-SHRIMPY-API-NONCE", nonce.ToString()); | |
client.DefaultRequestHeaders.Add("DEV-SHRIMPY-API-SIGNATURE", signature_b64); | |
var response = client.SendAsync(msg); | |
var responseString = response.Result.Content.ReadAsStringAsync().Result; | |
Console.WriteLine($"{responseString}"); | |
} | |
public static string GetSignature(string key, string message) | |
{ | |
using (var hmacsha256 = new HMACSHA256(Convert.FromBase64String(key))) | |
{ | |
byte[] hashmessage = hmacsha256.ComputeHash(Encoding.ASCII.GetBytes(message)); | |
return Convert.ToBase64String(hashmessage); | |
} | |
} | |
} | |
} |
Tried this code, as-is. Getting this error:
ProtocolViolationException: Cannot send a content-body with this verb-type.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this code, but there is a bug..
client.DefaultRequestHeaders.Add
Will keep appending the request headers when used in a loop..
DEV-SHRIMPY-API-NONCE: 1608932025545, 1608932026202, 1608932026254, 1608932026744, 1608932026771, 1608932026839, 1608932026905