1

我正在尝试使用以下 json 和 c# 代码更新 shopify 中现有变体的库存:

    var p = new
    {
            id = "220994352",
            inventory_quantity = "0"
    };

    string url = "https://somestore.myshopify.com/admin/variants/220994352.json";

    ShopifyAuthorizationState authState = (ShopifyAuthorizationState)HttpContext.Current.Session["shopify_auth_state"];
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    request.Method = "PUT";
    request.ContentType = "application/json";
    request.Headers.Add("X-Shopify-Access-Token", authState.AccessToken);

    var serializer = new JavaScriptSerializer();
    var json = serializer.Serialize(p);
    json = "{ variant: " + json + "}";

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


    string result = "";
    try
    {
        var response = (HttpWebResponse)request.GetResponse();
        using (Stream stream = response.GetResponseStream())
        {
            StreamReader sr = new StreamReader(stream);
            result = sr.ReadToEnd();
            sr.Close();
        }
    }
    catch (WebException ex)
    {
        WebHeaderCollection webHeaderCollection = ex.Response.Headers;
        foreach (var header in ex.Response.Headers.AllKeys)
        {
            Console.WriteLine(webHeaderCollection[header]);
        }
    }

但是,每次我运行它时,我都会收到一个 400 Bad 请求,但没有关于错误的详细信息。我可以毫无问题地阅读库存。

4

2 回答 2

1

您确定在授权应用程序时设置了您的范围权限以便对产品具有写入权限吗?

您能否发布一个指向原始 http 响应的链接以显示返回的确切内容,包括标题以查看那里是否有更多详细信息?

于 2012-07-25T15:12:55.757 回答
1

400 bad request 通常意味着正在发送的数据是无效的。并且查看您的 json 可能会对您有所帮助。尝试在您的 json 字符串中为变量加上引号。

于 2012-07-25T17:01:52.537 回答