【问题标题】:Shopify api updating variants returned errorShopify api 更新变体返回错误
【发布时间】:2016-08-16 09:32:32
【问题描述】:

我想更新商店产品的inventory_quantity,但出现错误提示

远程服务器返回错误:(400) Bad Request。

这就是我的实现方式。

ProductVariant product = new ProductVariant();
product.id = 1962693211;
product.inventory_quantity = 20;
product.inventory_management = "shopify";

string ordersApi = ConfigurationManager.AppSettings["Shopify_Api_Inventory"];
string url = ordersApi.Replace("{StoreName}", _cred.StoreName);
url = url.Replace("{id}", product.id.ToString());
string xmlStringResult = string.Empty;


try
{
    var req = (HttpWebRequest)WebRequest.Create(url);
    req.Method = "PUT";
    req.ContentType = "application/json";
    req.Credentials = GetCredential(url);
    req.PreAuthenticate = true;

    var json = JsonConvert.SerializeObject(product);
    json = "{ variant: " + json + "}";



    if (!String.IsNullOrEmpty(json))
    {
        using (var ms = new MemoryStream())
        {
            using (var writer = new StreamWriter(req.GetRequestStream()))
            {
                writer.Write(json);
                writer.Close();
            }
        }
    }

    using (var resp = (HttpWebResponse)req.GetResponse())
    {
        if (resp.StatusCode != HttpStatusCode.OK)
        {
            string message = String.Format("Call failed. Received HTTP {0}", resp.StatusCode);
            AppendLog(string.Format("Error: {0}", message));
            return xmlStringResult;
        }

        var sr = new StreamReader(resp.GetResponseStream());
        xmlStringResult = sr.ReadToEnd();
    }
}
catch (Exception ex)
{
    AppendLog(string.Format("Error: {0}", ex.Message));
}

提前感谢您的帮助。

【问题讨论】:

    标签: c# json json.net shopify inventory


    【解决方案1】:

    这行代码

    json = "{ variant: " + json + "}";
    

    产生下面的json

    { variant: {"id":1962693211,"inventory_quantity":20,"inventory_management":"shopify"}}
    

    这是无效的。有效的 json 应该是

    { "variant": {"id":1962693211,"inventory_quantity":20,"inventory_management":"shopify"}}
    

    所以你需要把上面这行代码改成这个

    json = "{ \"variant\": " + json + "}";
    

    【讨论】:

    • 我尝试更改它,但出现此错误:远程服务器返回错误:(403) Forbidden。
    • 您的 Shopify 应用是否有权更新库存?
    • 如何查看权限?
    • 老实说,我不确定该怎么做。也许这会有所帮助:help.shopify.com/api/guides/authentication/oauth,我猜你需要的是write_products 范围。
    • 似乎没有产品的写入权限,现在它正在工作。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2017-12-30
    • 1970-01-01
    • 2016-08-13
    • 2017-06-13
    • 2019-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多