Inhalte aufrufen

Profilbild

Can't Do StockQuanitity PATCH Using OData

API OData webapi web-api

  • Bitte melden Sie sich an, um eine Antwort zu verfassen.
6 Antworten zu diesem Thema

#1 HatemHusam

HatemHusam

    Advanced Member

  • Members
  • PunktPunktPunkt
  • 30 Beiträge

Geschrieben: 14 July 2019 - 13:53

I wrote a piece of code to GET the product ID by the SKU and then to update StockQuantity with using PATCH request.
 
The GET request is working as perfectly, but the PATCH request is returning an unauthorized error.
 
I've followed all instruction in the documentation but still, I'm not sure why I'm getting this error message.
 
 
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();





    }


#2 Marcus Gesing

Marcus Gesing

    SmartStore AG

  • Administrators
  • 3831 Beiträge

Geschrieben: 14 July 2019 - 17:27

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, example
Authorization: SmNetHmac1 8puWspyJ3j7mb3SLdsFRo7+5EOsEX783nBfBOyKuml4=

Marcus Gesing

Smartstore AG


#3 HatemHusam

HatemHusam

    Advanced Member

  • Members
  • PunktPunktPunkt
  • 30 Beiträge

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, example
Authorization: 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

 

2019-07-15-11-57-12-Feedback-Card-sln-2-



#4 HatemHusam

HatemHusam

    Advanced Member

  • Members
  • PunktPunktPunkt
  • 30 Beiträge

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 Marcus Gesing

Marcus Gesing

    SmartStore AG

  • Administrators
  • 3831 Beiträge

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 Marcus Gesing

Marcus Gesing

    SmartStore AG

  • Administrators
  • 3831 Beiträge

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 HatemHusam

HatemHusam

    Advanced Member

  • Members
  • PunktPunktPunkt
  • 30 Beiträge

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()
                ); 



Auch markiert mit einem oder mehrerer dieser Schlüsselwörter: API, OData, webapi, web-api