Inhalte aufrufen

Profilbild

Can't Update StockQuantity Using API

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: 09 July 2019 - 17:21

Hi, I was trying to update StockQuantity using API, the product shall be selected by the SKU.

 

Below is my code, but it failed to update the StockQuantity as required:

protected void Page_Load(object sender, EventArgs e)
    {

       

        string publicKey = "XYZ";
        string secretKey = "XYZ";
        string content = "{\"StockQuantity\":\"5\"}"; // assume that new stock is 5
        string accept = "application/json, text/javascript, */*";
        byte[] data = Encoding.UTF8.GetBytes(content);
        string contentMd5Hash = CreateContentMd5Hash(data);
        string method = "PUT";
        string timestamp = DateTime.UtcNow.ToString("o"); // 2013-11-11T19:44:04.9378268Z
        string url = "http://xyz.xyz.com:1260/odata/v1/Products$filter=Sku eq '1234'"; // assume that SKU is 1234

        var uri = new Uri(url);     // decode 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.Headers.Add("Content-MD5", contentMd5Hash); 


        using (var stream = request.GetRequestStream())
        {
            stream.Write(data, 0, data.Length);
        }


    }

    public string CreateContentMd5Hash(byte[] content)
    {
        string result = "";
        if (content != null && content.Length > 0)
        {
            using (var md5 = MD5.Create())
            {
                byte[] hash = md5.ComputeHash(content);
                result = Convert.ToBase64String(hash);
            }
        }
        return result;
    }

    public string CreateSignature(string secretKey, string messageRepresentation)
    {
        if (string.IsNullOrWhiteSpace(secretKey) || string.IsNullOrWhiteSpace(messageRepresentation))
            return "";

        string signature;
        var secretBytes = Encoding.UTF8.GetBytes(secretKey);
        var valueBytes = Encoding.UTF8.GetBytes(messageRepresentation);

        using (var hmac = new HMACSHA256(secretBytes))
        {
            var hash = hmac.ComputeHash(valueBytes);
            signature = Convert.ToBase64String(hash);
        }
        return signature;
    }

  • GalenKa gefällt das

#2 Marcus Gesing

Marcus Gesing

    SmartStore AG

  • Administrators
  • 3799 Beiträge

Geschrieben: 09 July 2019 - 18:48

For a partial upgrade, you need to use the PATCH method, not PUT. The StockQuantity property is of type int, not string. The exact error message would be helpful if something does not work.


Marcus Gesing

Smartstore AG


#3 HatemHusam

HatemHusam

    Advanced Member

  • Members
  • PunktPunktPunkt
  • 30 Beiträge

Geschrieben: 10 July 2019 - 09:02

For a partial upgrade, you need to use the PATCH method, not PUT. The StockQuantity property is of type int, not string. The exact error message would be helpful if something does not work.

 

 

Thank you Marcus, I adjusted for the method to patch and StockQuantity property to int .

 

The code doesn't throw any error and completed successfully but the StockQuantity is still not reflected in the Store, so I add HttpWebResponse to read the response from the store, but HttpWebResponse is throwing an error 401 Unauthorized, I used the same code mentioned in this page

http://docs.smartsto...0/Code Examples



#4 HatemHusam

HatemHusam

    Advanced Member

  • Members
  • PunktPunktPunkt
  • 30 Beiträge

Geschrieben: 10 July 2019 - 09:35

Store log is showing us an error as below

 

2019-07-10-11-33-18-View-log-entry-detai



#5 Marcus Gesing

Marcus Gesing

    SmartStore AG

  • Administrators
  • 3799 Beiträge

Geschrieben: 10 July 2019 - 11:33

Please check public and private access keys. The customer with this keys must be authorized on the plugin's configuration page to use the API.
A full working client can be found here.

Marcus Gesing

Smartstore AG


#6 HatemHusam

HatemHusam

    Advanced Member

  • Members
  • PunktPunktPunkt
  • 30 Beiträge

Geschrieben: 10 July 2019 - 16:58

 

Please check public and private access keys. The customer with this keys must be authorized on the plugin's configuration page to use the API.
A full working client can be found here.

 

 

 

Thank you that helps me a lot to understand better, I tried to update the stock level using the javascript example in github but it is giving me the below msg, does it mean that Stock Quantity couldn't be updated using API ?

 

2019-07-10-18-56-20-Smart-Store-Net-Web-



#7 HatemHusam

HatemHusam

    Advanced Member

  • Members
  • PunktPunktPunkt
  • 30 Beiträge

Geschrieben: 10 July 2019 - 21:31

Done! I used the Product ID and it is solved :) Thank you.




Auch markiert mit einem oder mehrerer dieser Schlüsselwörter: API