【发布时间】:2021-01-28 08:50:01
【问题描述】:
下面是我的代码。从 shopify 获取令牌工作正常。但是,在创建新产品时,它一直给我一个错误。我已经尝试了所有可能的方法,但仍然无法正常工作。任何建议将不胜感激。
以下是我调用 CreateNewProduct 方法的方法,该方法传递来自 shopify 的访问令牌和带有产品端点的商店名称。
CreateNewProduct(accessTokenDTO.access_token, "https://{myshopname}.myshopify.com/admin/api/2020-10/products.json");
方法如下。
public static void CreateNewProduct(string token, string Url)
{
Uri shopUri = new Uri(Url);
HttpWebRequest GETRequest = (HttpWebRequest)WebRequest.Create(shopUri);
GETRequest.ContentType = "application/json";
GETRequest.Headers.Add("X-Shopify-Access-Token", token);
GETRequest.PreAuthenticate = true;
GETRequest.Method = "PUT";
using (var streamWriter = new StreamWriter(GETRequest.GetRequestStream()))
{
string json = "{\"product\": { \"title\": \"Burton Custom Freestyle 151\", \"body_html\": \"<strong>Good snowboard!</strong>\", \"vendor\": \"Burton\", \"product_type\": \"Snowboard\", \"tags\": [ \"Barnes & Noble\", \"John's Fav\", \"\\Big Air\\]}";
streamWriter.Write(json);
streamWriter.Flush();
}
HttpWebResponse GETResponse = (HttpWebResponse)GETRequest.GetResponse();
var encoding = ASCIIEncoding.ASCII;
using (var reader = new System.IO.StreamReader(GETResponse.GetResponseStream(), encoding))
{
string responseText = reader.ReadToEnd();
Debug.WriteLine("Response Text: " + responseText);
}
GETResponse.Close();
}
【问题讨论】:
-
出现错误,不起作用......这些语句并没有太大帮助。你能在你的帖子中给出错误信息吗
-
异常抛出:System.dll 中的“System.Net.WebException” System.dll 中发生“System.Net.WebException”类型的未处理异常远程服务器返回错误:(400)错误要求。 @贾瓦德
标签: c# html json shopify httpwebrequest