0

I am using C # to search for orders from last month (last 30 days). Magento version 2 using REST, I am using the call as follows:

http://magentoserver/index.php/rest/V1/orders?searchCriteria[filter_groups][0][filters][0][field]=created_at&searchCriteria[filter_groups][0][filters][0][value]=2017年08月01日&searchCriteria[filter_groups][0][filters][0][condition_type]=gt

The remote server returned an error: (401) Unauthorized.

If the call is:

http://magentoserver/index.php/rest/V1/orders?searchCriteria=

The server returns all orders.

It's not an authorization error, I really don't find the right way to build the search criteria. I don't find any documentation that explains how to do it

asked Aug 18, 2017 at 10:15
2
  • Welcome to Magento StackExchange! could you specify what version of Magento is this question about? Commented Aug 18, 2017 at 10:25
  • Magento version 2.0.13 Commented Aug 21, 2017 at 9:14

1 Answer 1

0

I'm building the code like this:

 public class ApiClient
{
 public ApiClient(string magentoServer, string consumerKey, string consumerSecret, string accessToken, string accessTokenSeccret)
 {
 MagentoServer = magentoServer;
 ConsumerKey = consumerKey;
 ConsumerSecret = consumerSecret;
 AccessToken = accessToken;
 AccessTokenSecret = accessTokenSeccret;
 }
 #region Request
 public HttpWebRequest CreateAuthorizedRequest(string url, string requestMethod, string filter)
 {
 HttpWebRequest request = null;
 if (filter == null)
 request = (HttpWebRequest)WebRequest.Create(url);
 else
 request = (HttpWebRequest)WebRequest.Create(url + "?" + filter.ToString());
 OAuthBase oAuth = new OAuthBase();
 string nonce = oAuth.GenerateNonce();
 string timeStamp = oAuth.GenerateTimeStamp();
 string parameters;
 string normalizedUrl;
 string signature = oAuth.GenerateSignature(new Uri(url), ConsumerKey, ConsumerSecret,
 AccessToken, AccessTokenSecret, requestMethod, timeStamp, nonce, OAuthBase.SignatureTypes.HMACSHA1,
 out normalizedUrl, out parameters);
 StringBuilder sb = new StringBuilder("OAuth ");
 sb.AppendFormat("oauth_token=\"{0}\",", AccessToken);
 sb.AppendFormat("oauth_version=\"{0}\",", "1.0");
 sb.AppendFormat("oauth_signature_method=\"{0}\",", "HMAC-SHA1");
 sb.AppendFormat("oauth_nonce=\"{0}\",", nonce);
 sb.AppendFormat("oauth_timestamp=\"{0}\",", timeStamp);
 sb.AppendFormat("oauth_consumer_key=\"{0}\",", ConsumerKey);
 sb.AppendFormat("oauth_signature=\"{0}\"", signature);
 request.Headers[HttpRequestHeader.Authorization] = sb.ToString();
 request.Method = requestMethod;
 request.Accept = "application/json";
 request.ContentType = "application/json";
 request.KeepAlive = true;
 return request;
 }
 public string FetchRequest(HttpWebRequest request)
 {
 try
 {
 string responseText = string.Empty;
 HttpWebResponse response = (HttpWebResponse)request.GetResponse();
 if (response.StatusCode == HttpStatusCode.OK)
 {
 using (Stream responseStream = response.GetResponseStream())
 {
 using (StreamReader responseReader = new StreamReader(responseStream))
 {
 responseText = responseReader.ReadToEnd();
 return responseText;
 }
 }
 }
 return responseText;
 }
 catch (WebException ex)
 {
 Console.WriteLine("Error: " + ex.Message);
 return null;
 }
 }
 public static string GetValues(string _call, string method)
 {
 ApiClient client = new ApiClient(MagentoServer, ConsumerKey, ConsumerSecret, AccessToken, AccessTokenSecret);
 string url = url_base + _call;
 string t = null;
 HttpWebRequest myReq = client.CreateAuthorizedRequest(url, method, null);
 t = client.FetchRequest(myReq);
 if (t != null)
 t = t.Replace("\\/", "/");
 return t;
 }

The call it's made like:

 public string url = "http://" + magentoServer;
 public string url_base = "/index.php/rest/V1/";
 string _call = "orders?";
 _call += "searchCriteria[currentPage]=1&";
 _call += "searchCriteria[pageSize]=1";
 string _resp = Magento2Form.GetValues(_call, "GET", null);

This single call only with pageSize and currentPage gives me the answer:

Error: The remote server returned an error: (401) Unauthorized.
answered Aug 21, 2017 at 13:58

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.