我有一个使用C#编写的完美代码,该代码曾经每天用于从Shopify API中获取数据,但在过去的几天中,它开始给我带来错误。我在追查到底是什么原因造成了问题。
如果我通过Web浏览器或RESTAPI Client进行API调用,则每次数据检索都会成功。
而且,由于我对这段代码没有好运,所以我做了从C#到Python的代码迁移,当我运行Python脚本时,数据检索成功了,没有任何错误。
我的C#代码看起来像这样:
public override void CreateNewOutputRows()
{
string tablename = "products";
string filepath = Variables.DATAFILEPATH + "SHOPIFY_" + tablename.ToUpper() + ".csv";
string apiKey = "xxxxxxxxxxxx";
string password = "xxxxxxxxx";
string json = GetSomeREST(apiKey, password, "admin/" + tablename);
KSONConverter(json, filepath);
}
private string GetSomeREST(string apiKey, string password, string path, int offset = 0, string listId = "")
{
var uri = string.Format("https://xxxxxxx.myshopify.com/admin/products.json");
try
{
using (var webClient = new System.Net.WebClient())
{
//webClient.Headers.Add("Accept", "application/json");
//webClient.Headers.Add("Authorization", "apikey " + apiKey);
webClient.Headers.Add("X-Shopify-Access-Token", password);
return webClient.DownloadString(uri);
}
}
catch (System.Net.WebException we)
{
using (var sr = new System.IO.StreamReader(we.Response.GetResponseStream())
//getting error on the above line of code
{
return sr.ReadToEnd();
}
}
}
public void KSONConverter(string json, string filepath)
{
// System.IO.File.AppendAllText(filepath, json);
RootObject ro = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(json);
string csv = "";
foreach(Product p in ro.products)
{
csv = csv + p.id + "|";
csv = csv + p.title.Replace('|',' ') + "|";
//csv = csv + p.body_html + "|";
csv = csv + p.vendor.Replace('|', ' ') + "|";
csv = csv + p.product_type.Replace('|', ' ') + "|";
csv = csv + p.created_at + "|";
csv = csv + p.handle + "|";
csv = csv + p.updated_at + "|";
csv = csv + p.published_at + "|";
csv = csv + p.template_suffix + "|";
csv = csv + p.published_scope + "|";
csv = csv + p.tags;
csv = csv + "\n";
}
System.IO.File.WriteAllText(filepath, csv);
}
我的预期输出应该是一个CSV文件,其中包含从Shopify API提取的数据,但是,我在上面提到的代码部分收到了错误“对象引用未设置为对象的实例” /在上述代码行中获取错误