public void UpdateQTY(string ProductId, int CurrentStock) { string publicKey = "xyz"; string secretKey = "xyz"; string method = "patch"; string accept = "application/json, text/javascript, */*"; string timestamp = DateTime.UtcNow.ToString("o"); // FOR PATCH REQUEST string content = JsonConvert.SerializeObject(new { StockQuantity = 5 }); // assume stock is new stock is 5 byte[] data = Encoding.UTF8.GetBytes(content); string contentMd5Hash = CreateContentMd5Hash(data); string url = "http://xyz:1260/odata/v1/Products("+ProductId+")"; var uri = new Uri(url); if (uri.Query != null && uri.Query.Length > 0) { url = string.Concat(uri.GetLeftPart(UriPartial.Path), HttpUtility.UrlDecode(uri.Query)); } var messageRepresentation = string.Join("\n", method.ToLower(), "", accept.ToLower(), url.ToLower(), timestamp, publicKey.ToLower() ); string signature = CreateSignature(secretKey, messageRepresentation); var request = (HttpWebRequest)WebRequest.Create(url); request.Method = method; request.Accept = accept; request.Headers.Add("Accept-Charset", "UTF-8"); request.Headers.Add("SmartStore-Net-Api-PublicKey", publicKey); request.Headers.Add("SmartStore-Net-Api-Date", timestamp); request.Headers.Add("Authorization", "SmNetHmac1" + signature); request.ContentLength = data.Length; request.ContentType = "application/json; charset=utf-8"; request.Headers.Add("Content-MD5", contentMd5Hash); var stream = request.GetRequestStream(); stream.Write(data, 0, data.Length); stream.Close(); WebResponse response = request.GetResponse(); Console.WriteLine(((HttpWebResponse)response).StatusDescription); // Get the stream containing content returned by the server. // The using block ensures the stream is automatically closed. using (stream = response.GetResponseStream()) { // Open the stream using a StreamReader for easy access. StreamReader reader = new StreamReader(stream); // Read the content. string responseFromServer = reader.ReadToEnd(); // Display the content. Console.WriteLine(responseFromServer); } // Close the response. response.Close(); }
Can't Do StockQuanitity PATCH Using OData
#1
Geschrieben: 14 July 2019 - 13:53
#2
Geschrieben: 14 July 2019 - 17:27
Authorization: SmNetHmac1 8puWspyJ3j7mb3SLdsFRo7+5EOsEX783nBfBOyKuml4=
Marcus Gesing
Smartstore AG
#3
Geschrieben: 15 July 2019 - 10:07
It would be helpful to provide the exact error message including the response header, especially HmacResultId and HmacResultDesc that gives a hint to details about the error. contentMd5Hash is missing in your message representation. And it looks like there's a missing space character in your authorization header code, exampleAuthorization: SmNetHmac1 8puWspyJ3j7mb3SLdsFRo7+5EOsEX783nBfBOyKuml4=
Hi @Marcus, I fixed both contentMd5Hash & the space but I'm still getting the same error ( please note that it is working fine using GET instead of patch )
here is a full screenshot from the error
#4
Geschrieben: 15 July 2019 - 10:23
I tested the same using third party website such as apirequest.io but I got the same error Unauthorized, I believe this error is not from my code itself. what do think? shall I connect directly and modify the DB ?
The response header is
{
"expires": "-1",
"smartstore-net-api-date": "2019-07-15T09:17:57.9488929Z",
"smartstore-net-api-maxtop": "120",
"smartstore-net-api-hmacresultdesc": "InvalidSignature",
"smartstore-net-api-hmacresultid": "4",
"cache-control": "no-cache",
"pragma": "no-cache",
"content-type": null
}
But I used the exactly same signature for GET request and it is authorized and works fine!
#5
Geschrieben: 15 July 2019 - 10:36
Probably the MD5 hash is wrong. The API configuration option to authentification without MD5 hash should be deactivated if you pass a MD5 hash in your signature.
Marcus Gesing
Smartstore AG
#6
Geschrieben: 15 July 2019 - 10:53
Changing values directly in the database is not a good idea. The application never notices this and may react unexpectedly.
Marcus Gesing
Smartstore AG
#7
Geschrieben: 15 July 2019 - 11:22
Thanks Marcus, Finally it is solved after more than three days
Issue was with MD5, even it MD5 validation was turned off the system was showing that error because of the time stamp messageRepresentation , the were 2 (") on the second line, when I added MD5 hash issue is solved.
I'm writing this hopefully someone else will get some help, appreciate if you can clarify this in the documentation as well. Thank you.
var messageRepresentation = string.Join("\n", method.ToLower(), contentMd5Hash ?? "", accept.ToLower(), url.ToLower(), timestamp, publicKey.ToLower() );