Ankur Vishnoi
Ankur Vishnoi

Reputation: 81

Shopify - 401 error during webrequest

I am facing some serious issue with Shopify during web request to get order list from my application while I am getting fine with direct hit on browser.

here is the code :



    private const string APIKey = "[DELETED]";
        private const string APIPassword = "[DELETED]";
        private const string APISecrateKey = "[DELETED]";
        private const string StoreName = "epronto-2";
        private const string OrderURL = "https://" + APIKey + ":" + APIPassword + "@" + StoreName + ".myshopify.com/admin/orders.json";
        public string gethttpResponse(){
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "GET";
            req.ContentType = "application/json";
            req.Headers.Add("X-Shopify-Access-Token", APISecrateKey);
            string text = string.Empty;
            try{
                var response = (HttpWebResponse)req.GetResponse();
                using (var sr = new StreamReader(response.GetResponseStream()))
                {
                    text = sr.ReadToEnd();
                }
            }
            catch{}
            return text;
        }

can you please suggest what i am missing here.

What I have tried:

actually, I am getting data from the private app,I read many articles and found there is no required for permanent access_token via OAuth API,

so I added req.Headers.Add("X-Shopify-Access-Token", APISecrateKey); this line , but I could not work for me.

Upvotes: 1

Views: 826

Answers (1)

WHM
WHM

Reputation: 31

try adding req.credentials(apikey,apipassword);

I was having the same issue but had tried a mix of approaches and inadvertently had both in my code that was working. I took that line out thinking it was superfluous and could not figure out why my code broke until I saw your post and tried adding it back.

my code is now"

 WebRequest OrderRequest = WebRequest.Create("<url like you build>");
   OrderRequest.Credentials = new NetworkCredential"APIKEY", "APIpassword");
   response = (HttpWebResponse)OrderRequest.GetResponse();

after a bit of testing, I altered your code and this is working:

 public static string gethttpResponse(Uri url)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "GET";
            req.ContentType = "application/json";
            //req.Headers.Add("X-Shopify-Access-Token", APISecrateKey);
            req.Credentials = new NetworkCredential(APIKey, APIPassword);
            string text = string.Empty;
            try
            {
                var response = (HttpWebResponse)req.GetResponse();
                using (var sr = new StreamReader(response.GetResponseStream()))
                {
                    text = sr.ReadToEnd();
                }
            }
            catch (Exception a)
            {
                Utility.LogMessage(a.ToString());
            }
            return text;
        }

Upvotes: 3

Related Questions