this is driving me nuts. Heres what they tell me is working in C#:
static void Main(string[] args)
{
string json = “{” +
" “machineSns”: [" +
" “IL4600E028"” +
" ]," +
" “dateFrom”: “2021-01-01T00:00:00-00:00”," +
" “dateTo”: “2022-01-01T00:00:00-00:00”," +
" “startRow”: 0," +
" “endRow”: 99," +
" “selectedKpiTab”: “”" +
“}”;
var data = new StringContent(json, Encoding.UTF8, “application/json”);
var url = “https://printos.api.hp.com/printbeat/externalApi/gsb/scitexJobs/records”;
var client = new HttpClient();
CreateHmacHeaders("POST", "/externalApi/gsb/scitexJobs/records", client);
var response = client.PostAsync(url, data).Result;
string result = response.Content.ReadAsStringAsync().Result;
}
private static void CreateHmacHeaders(string method, string path, HttpClient client)
{
string key = "<My key>";
string secret = "<My secret>";
string timeStamp = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ");
string stringToSign = method + " " + path + timeStamp;
HMACSHA1 hmac = new HMACSHA1(Encoding.UTF8.GetBytes(secret));
byte[] bytes = hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign));
string signature = BitConverter.ToString(bytes).Replace("-", string.Empty).ToLower();
string auth = key + ":" + signature;
client.DefaultRequestHeaders.Add("x-hp-hmac-authentication", auth);
client.DefaultRequestHeaders.Add("x-hp-hmac-date", timeStamp);
client.DefaultRequestHeaders.Add("x-hp-hmac-algorithm", "SHA1");
}
}
}
…and heres what I have:
var json as string = “{” +_
" ““machineSns””: [" +_
" ““IL4600E028"” " +_
" ],” +_
" ““dateFrom””:2021-01-01T00:00:00-00:00," +_
" ““dateTo””:2022-01-01T00:00:00-00:00," +_
" ““startRow””: 0," +_
" ““endRow+””: 99," +_
" ““selectedKpiTab+””:"""" " +_
“}”
var sendMethod as string = “POST”
var hash As MemoryBlock
var path as string = “/externalApi/gsb/scitexJobs/records”
var sig As String
var d as DateTime = DateTime.Now(New TimeZone(“Zulu”))
var timestamp as string =d.SQLDateTime
var stringToSign as string = "SHA1 "+path+timestamp+“Z”
hash = Crypto.Hash(stringToSign, Crypto.HashAlgorithms.SHA1)
sig=EncodeHex(Crypto.HMAC(apisecret,hash, Crypto.HashAlgorithms.SHA1))
sig=sig.ReplaceAll("-","")
sig=sig.Lowercase
var auth as string = apikey + “:” + sig
me.SetRequestContent(json, “application/json”)
me.RequestHeader(“X-HP-HMAC-Authentication:”) = auth
me.RequestHeader(“X-HP-HMAC-Date:”) = timestamp+“Z”
'Self.RequestHeader(“X-HP-HMAC-Algorithm:”) = “SHA1”
var payload as string =“https://printos.api.hp.com/printbeat/externalApi/gsb/scitexJobs/records”
me.Send(sendMethod, payload)
If I dont comment out the algorithm assignment, it gives me an error “HMAC algorith invalid” 400. Commented, it uses SHA1 as the default so that goes through and I get a 401 “Access denied for this resource” for whatever combination I think of sending the datat through. Anyone have any thoughts?