Magento 2 Rest Get Orders C#
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
magento2 orders rest-api
bumped to the homepage by Community♦ 7 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
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
magento2 orders rest-api
bumped to the homepage by Community♦ 7 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
Welcome to Magento StackExchange! could you specify what version of Magento is this question about?
– diazwatson
Aug 18 '17 at 10:25
Magento version 2.0.13
– Luís
Aug 21 '17 at 9:14
add a comment |
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
magento2 orders rest-api
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
magento2 orders rest-api
magento2 orders rest-api
edited Aug 21 '17 at 13:30
Luís
asked Aug 18 '17 at 10:15
LuísLuís
63
63
bumped to the homepage by Community♦ 7 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ 7 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
Welcome to Magento StackExchange! could you specify what version of Magento is this question about?
– diazwatson
Aug 18 '17 at 10:25
Magento version 2.0.13
– Luís
Aug 21 '17 at 9:14
add a comment |
Welcome to Magento StackExchange! could you specify what version of Magento is this question about?
– diazwatson
Aug 18 '17 at 10:25
Magento version 2.0.13
– Luís
Aug 21 '17 at 9:14
Welcome to Magento StackExchange! could you specify what version of Magento is this question about?
– diazwatson
Aug 18 '17 at 10:25
Welcome to Magento StackExchange! could you specify what version of Magento is this question about?
– diazwatson
Aug 18 '17 at 10:25
Magento version 2.0.13
– Luís
Aug 21 '17 at 9:14
Magento version 2.0.13
– Luís
Aug 21 '17 at 9:14
add a comment |
1 Answer
1
active
oldest
votes
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.
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "479"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f189633%2fmagento-2-rest-get-orders-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
add a comment |
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.
add a comment |
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.
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 '17 at 13:58
LuísLuís
63
63
add a comment |
add a comment |
Thanks for contributing an answer to Magento Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f189633%2fmagento-2-rest-get-orders-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Welcome to Magento StackExchange! could you specify what version of Magento is this question about?
– diazwatson
Aug 18 '17 at 10:25
Magento version 2.0.13
– Luís
Aug 21 '17 at 9:14