Discussions

Ask a Question
Back to All

Where is wrong ?

Hello.

I'm fighting with these 10 lines of code from the morning.

I'm looking for to obtain the list of payments Ihave done from sandbox app.

This is c# , but any language goes well.

string host = "staging.authservices.satispay.com";
string keyId = System.IO.File.ReadAllText("satispaykeyid.txt");
string pemText = System.IO.File.ReadAllText("satispay_private.pem");
string base64Key = pemText.Replace("-----BEGIN PRIVATE KEY-----", "").Replace("-----END PRIVATE KEY-----", "").Replace("\r", "").Replace("\n", "");
string urlapi = "/g_business/v1/payments";
var bodyObject = new Dictionary<string, object>();
var body = JsonSerializer.Serialize(bodyObject);
var sha256 = SHA256.Create();
var hashBytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(body));
var digest = "SHA-256=" + Convert.ToBase64String(hashBytes);
string date = DateTime.UtcNow.ToString("r");
string message = $"(request-target): post {urlapi}\n" + $"host: {host}\n" + $"date: {date}\n" +$"digest: {digest}";
byte[] privateKeyBytes = Convert.FromBase64String(base64Key);
byte[] dataBytes = Encoding.UTF8.GetBytes(message);
string signature = "";
using (var rsa = RSA.Create())
{
rsa.ImportPkcs8PrivateKey(privateKeyBytes, out
);
var sha256_2 = SHA256.Create();
byte[] hash = sha256_2.ComputeHash(dataBytes);
byte[] signatureBytes = rsa.SignHash(hash, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
signature = Convert.ToBase64String(signatureBytes);
}
string authorization = $"Signature keyId="{keyId}", algorithm="rsa-sha256", headers="(request-target) host date digest", signature="{signature}"";
HttpClient clientz = new HttpClient();
HttpRequestMessage msg = new HttpRequestMessage();
msg.RequestUri = new Uri($"https://{host}{urlapi}");
msg.Headers.Add("Accept", "application/json");
msg.Headers.Add("Host", host);
msg.Headers.Add("Date", date);
msg.Headers.Add("Digest", digest);
msg.Headers.Add("Authorization", "Bearer " + authorization);
msg.Method = HttpMethod.Get;
//msg.Content = new StringContent(body, Encoding.UTF8, "application/json");
//clientz.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authorization);
HttpResponseMessage responseg = await clientz.SendAsync(msg);
string resultx = await responseg.Content.ReadAsStringAsync();

//always unauthorized !!!!!!!


Do you have idea why ?


Thanks