0

How to get customer orders on the base of customer token in magento 2?Please help me.Thanks in Advance

2 Answers 2

1
Url: http://192.168.1.74/B2B/Magento/index.php/rest/V1/orders?searchCriteria[pageSize]=25&searchCriteria[currentPage]=1&searchCriteria[filterGroups][0][filters][0][value]=128&searchCriteria[filterGroups][0][filters][0][field]=customer_id&searchCriteria[filterGroups][0][filters][0][conditionType]=eq

  method : GET

Headers

Authorization :: Bearer <Admintokenvalue>
2
  • I want to access orders without using admin token. Commented May 29, 2018 at 8:28
  • It will be not possible using that service as it need admin token according to ACL.xml. so write anonymous service using the same logic Commented May 29, 2018 at 9:05
0

First you need to get accesstoken of admin and then needs to call order api with passing that AccessToken.

Code for get admin accesstoken :

$userData = array("username" => "admin", "password" => "admin123");
$ch = curl_init("http://yourwebsiteurl/index.php/rest/V1/integration/admin/token");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($userData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Content-Lenght: " . strlen(json_encode($userData))));

$token = curl_exec($ch);

Get order details :

$ch = curl_init("http://yourwebsiteurl/index.php/rest/V1/orders/1");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));

$result = curl_exec($ch);

$result = json_decode($result, 1);
echo '<pre>';print_r($result);
5
  • without admin token,it is possible? Commented May 29, 2018 at 5:19
  • Can, try this code : domain.com/index.php/rest/V1/orders?searchCriteria[f‌​ilter_groups][0][fil‌​ters][0][field]=cust‌​omer_email&searchCri‌​teria[filter_groups]‌​[0][filters][0][valu‌​e][email protected]&se‌​archCriteria[filter_‌​groups][0][filters][‌​0][condition_type]=e‌​q&searchCriteria[fil‌​ter_groups][1][filte‌​rs][0][field]=status‌​&searchCriteria[filt‌​er_groups][1][filter‌​s][0][value]=pending‌​&searchCriteria[filt‌​er_groups][1][filter‌​s][0][condition_type‌​]=eq Commented May 29, 2018 at 5:32
  • it show "message":"Consumer is not authorized to access %resources","parameters":{"resources":"Magento_Sales::sales". Commented May 29, 2018 at 5:37
  • you changed the example of '[email protected]' ? Commented May 29, 2018 at 5:39
  • Yes, i will change,but showing same response Consumer is not authorized. Commented May 29, 2018 at 5:52

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.