I had a perfectly working code, written in C#, which used to fetch data from Shopify API on daily basis but for the past few days it has started giving me an error. I am having issues with tracing what exactly could be the source of this problem.
If I make API calls through Web Browser or RESTAPI Client, data retrieval is successful everytime.
Also, Since I was having no luck with this code, I did a code migration from C# to Python and when I ran my Python script, data retrieval was successful without any errors.
My C# code looks something like this:
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);
}
My expected output should be a CSV file, containing the data fetched from Shopify API but instead, I am getting the error "Object reference not set to an instance of an object" on the part of code mentioned above //getting error on the above line of code