1

Return order details in REST API Magento-2.2 get below error when tried to access using customer token, But it is working with admin token in salesOrderRepositoryV1

vendor_name/module_name/model/EmcOrderRepository.php

public function get($id)
{
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $order = $objectManager->create('Magento\Sales\Api\Data\OrderInterface')->load($id);
    $data = $order->getData();
    return $data;
}

And also I tried in many ways, But the result is same as Authorization error,

{
   "message": "No such entity with %fieldName = %fieldValue",
   "parameters": {
   "fieldName": "orderId",
   "fieldValue": "2"
},
"trace": "#0 /var/www/html/projectname/vendor/magento/module-sales/Model/ResourceModel/Order/Plugin/Authorization.php(45): Magento\Framework\Exception\NoSuchEntityException::singleField('orderId', '2')\n#1 .............
1

yes, in magento 2 default order detail API is only accessible using the admin token, if you need to do it with customer token you need to develop a custom API for that, recently i have developed a custom order detail api for customer, if you need that code i can share it with you here.

CODE FOR CUSTOM ORDER DETAIL API WORKS WITH CUSTOMER TOKEN

  1. Vendor/Sales/registration.php

    <?php
    
    \Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_Sales',
    __DIR__
    );
    
  2. Vendor/Sales/etc/module.xml

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Vendor_Sales" setup_version="1.0.0" schema_version="1.0.0">
    </module>
    

  3. Vendor/Sales/etc/webapi.xml

    <?xml version="1.0"?>
        <routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
    <!-- Routing define -->
    <route url="/V1/order/:orderId/mine" method="GET">
        <service class="Vendor\Sales\Api\MyOrdersInterface" method="getDetail"/>
        <resources>
            <resource ref="self" />
        </resources>
        <data>
            <parameter name="customerId" force="true">%customer_id%</parameter>
        </data>  
    </route>
    

  4. Vendor/Sales/etc/di.xml

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
    <preference 
        for="Vendor\Sales\Api\MyOrdersInterface"
        type="Vendor\Sales\Model\MyOrders" />
    <virtualType name="orderMetadata" type="Magento\Sales\Model\ResourceModel\Metadata">
        <arguments>
            <argument name="resourceClassName" xsi:type="string">Magento\Sales\Model\ResourceModel\Order</argument>
            <argument name="modelClassName" xsi:type="string">Magento\Sales\Model\Order</argument>
        </arguments>
    </virtualType>
    <type name="Vendor\Sales\Model\MyOrders">
        <arguments>
            <argument name="metadata" xsi:type="object">orderMetadata</argument>
        </arguments>
    </type>
    

  5. Vendor/Sales/Api/MyOrdersInterface.php

    <?php
    
    namespace Vendor\Sales\Api;
    
    
    /**
     * Get Customers Order List
     */
    interface MyOrdersInterface {
    
    /**
     * Return array
     *
     * @api
     * @param string $orderId
     * @param int $customerId
     * @return \Magento\Sales\Api\Data\OrderInterface Order interface.
     * @throws \Magento\Framework\Exception\InputException
     */
    public function getDetail($orderId, $customerId);
    }
    
  6. Vendor/Sales/Model/MyOrders.php

    <?php
    
    namespace Vendor\Sales\Model;
    
    use Vendor\Sales\Api\MyOrdersInterface;
    use Magento\Framework\Exception\InputException;
    use \Magento\Store\Model\StoreManagerInterface;
    use Magento\Framework\Exception\NoSuchEntityException;
    use Magento\Sales\Api\Data\OrderExtensionFactory;
    use Magento\Sales\Api\Data\OrderExtensionInterface;
    use Magento\Sales\Api\Data\OrderInterface;
    use Magento\Sales\Api\Data\ShippingAssignmentInterface;
    use Magento\Sales\Model\Order\ShippingAssignmentBuilder;
    use Magento\Sales\Model\ResourceModel\Metadata;
    use Magento\Framework\App\ObjectManager;
    
    /**
     * Implementation class of contract.
     */
    class MyOrders implements MyOrdersInterface
    {
    
    /**
     * store config
     * @var \Magento\Framework\App\Config\ScopeConfigInterface
     */
    protected $_scopeConfig;
    protected $_orderCollectionFactory;
    protected $orders;
    
    /**
     * @var Metadata
     */
    protected $metadata;
    
    
    /**
     * @var OrderInterface[]
     */
    protected $registry = [];
    
    /**
     * @var ShippingAssignmentBuilder
     */
    private $shippingAssignmentBuilder;
    
    
    /**
     * Constructor
     *
     * @param Metadata $metadata
     * @param \Magento\Sales\Api\Data\OrderExtensionFactory|null $orderExtensionFactory
     */
    
    public function __construct(
         \Magento\Catalog\Api\ProductRepositoryInterface $productrepositoryinterface,
         \Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
         \Vendor\Sales\Api\Data\MyOrdersdataInterfaceFactory $dataFactory,
         \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory,
          StoreManagerInterface $storeManager,
         \Magento\Sales\Model\OrderRepository $orderRepositoryModel,
          Metadata $metadata,
         \Magento\Sales\Api\Data\OrderExtensionFactory $orderExtensionFactory = null
    
    
    ) {
        $this->orderRepository = $orderRepository;
        $this->dataFactory = $dataFactory;
        $this->_orderCollectionFactory = $orderCollectionFactory;
        $this->productrepositoryinterface = $productrepositoryinterface;
        $this->_storeManager = $storeManager;
        $this->metadata = $metadata;
        $this->orderRepositoryModel = $orderRepositoryModel;
        $this->orderExtensionFactory = $orderExtensionFactory ?: ObjectManager::getInstance()
            ->get(\Magento\Sales\Api\Data\OrderExtensionFactory::class);
    }
    
    
    /**
     * load entity
     *
     * @param string $orderId
     * @param int $customerId
     * @return \Magento\Sales\Api\Data\OrderInterface
     * @throws \Magento\Framework\Exception\InputException
     * @throws \Magento\Framework\Exception\NoSuchEntityException
     */
    
    public function getDetail($orderId, $customerId) {
    
      if (!$orderId) {
            throw new InputException(__('Id required'));
        }
        if (!isset($this->registry[$orderId])) {
    
            /** @var OrderInterface $entity */
            $entity = $this->metadata->getNewInstance()->loadByIncrementId($orderId);
            if (!$entity->getEntityId()) {
                throw new NoSuchEntityException(__('Requested entity doesn\'t exist'));
            }
            $this->setShippingAssignments($entity);
            $this->registry[$orderId] = $entity;
        }
        return $this->registry[$orderId];
    
    }
    
    
    /**
     * @param OrderInterface $order
     * @return void
     */
    public function setShippingAssignments(OrderInterface $order)
    {
        /** @var OrderExtensionInterface $extensionAttributes */
        $extensionAttributes = $order->getExtensionAttributes();
    
        if ($extensionAttributes === null) {
            $extensionAttributes = $this->orderExtensionFactory->create();
        } elseif ($extensionAttributes->getShippingAssignments() !== null) {
            return;
        }
        /** @var ShippingAssignmentInterface $shippingAssignment */
        $shippingAssignments = $this->getShippingAssignmentBuilderDependency();
        $shippingAssignments->setOrderId($order->getEntityId());
        $extensionAttributes->setShippingAssignments($shippingAssignments->create());
        $order->setExtensionAttributes($extensionAttributes);
    }
    
    public function getShippingAssignmentBuilderDependency()
    {
        if (!$this->shippingAssignmentBuilder instanceof ShippingAssignmentBuilder) {
            $this->shippingAssignmentBuilder = \Magento\Framework\App\ObjectManager::getInstance()->get(
                \Magento\Sales\Model\Order\ShippingAssignmentBuilder::class
            );
        }
        return $this->shippingAssignmentBuilder;
    }
    
    }
    
  • I wrote custom API, But Magento core not allow to access model functionality. If you did this please share the code. – Gnanasekaran Loganathan May 23 '18 at 13:25
  • Added my code, but one thing is that in this model code might be some of constructor are not in use in the class. That,s all and please accept the answer if it helps you out :) – Deepak May 24 '18 at 6:04
  • I think some files you missed to mentioned here. – Gnanasekaran Loganathan May 25 '18 at 14:35
  • ohh sorry that is not needed, please remove that preference. i also updated my answer – Deepak May 26 '18 at 6:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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