cancel
Showing results for 
Search instead for 
Did you mean: 

How to get CustomerId in API in magento 2?

SOLVED

How to get CustomerId in API in magento 2?

Magento didn't load the customer session in API as used in web session based authentications.

 

and i am aware about the API

 

<magento.host>/rest/V1/customers/me

My question is, how do I make that call with PHP, so my code captures the customer ID and can use it.

 

How do I make a call to that URL pro-grammatically and capture the User ID into a PHP variable?

1 ACCEPTED SOLUTION

Accepted Solutions

Re: How to get CustomerId in API in magento 2?

Finally got the solution, i am trying my best to explain it.

so here's how Magento works !!

Magento gets the customer data with passing anything except token value in API call.

In between magento calls some API related controllers for the same

vendor/magento/module-customer/etc/webapi.xml

<route url="/V1/customers/me" method="GET">
        <service class="Magento\Customer\Api\CustomerRepositoryInterface" method="getById"/>
        <resources>
            <resource ref="self"/>
        </resources>
        <data>
            <parameter name="customerId" force="true">%customer_id%</parameter>
        </data>
 </route>

in this API call magento get customer data based on token.

Based on API url url="/V1/customers/me" magento calls function from

Magento calls In-between call of dispatch function.

vendor/magento/module-webapi/Controller/Rest.php

 public function dispatch(\Magento\Framework\App\RequestInterface $request)
 {
     /** In between code **/
     // In the same file     $this->processApiRequest();

 }
 protected function processApiRequest()
 {      $inputParams = $this->getInputParamsResolver()->resolve();
 }

And this resolve() function calls override() function

vendor/magento/module-webapi/Controller/Rest/InputParamsResolver.php

 public function resolve()
 {      $inputData = $this->paramsOverrider->override($inputData, $route->getParameters());
 }

and overide() function calls getOverriddenValue() function

vendor/magento/module-webapi/Controller/Rest/ParamsOverrider.php

 public function override(array $inputData, array $parameters)
 {      $value = $this->paramOverriders[$paramValue]->getOverriddenValue();
 }

getOverriddenValue() calls ParamOverriderInterface

vendor/magento/framework/Webapi/Rest/Request/ParamOverriderInterface.php

  • To Override parameter values
  • Parameters in the webapi.xml can be forced. This ensures that on specific routes, a specific value is always used.
  • For instance, if there is a ".../me/..." route, the route should use only user information specific to the
  • currently logged in user. More specifically, if there was a "/customers/me/addresses" route, the service method
  • invoked could have a signature of "getAddresses($customerId)", but in the webapi.xml, the $customerId parameter
  • would be forced to be the customer id of the current authenticated user.
  • The forced override parameter configuration is in the webapi.

   <data>
     <parameter name="customer.id" force="true">%customer_id%</parameter>
   </data>

and for business logic of this function is in file -

vendor/magento/module-webapi/Controller/Rest/ParamOverriderCustomerId.php

   /**
    * {@inheritDoc}
    */
   public function getOverriddenValue()
   {
       if ($this->userContext->getUserType() === UserContextInterface::USER_TYPE_CUSTOMER) {
           return $this->userContext->getUserId();
   }
   return null;
   }

So this is how Magento get Customer Id between API calls !!

View solution in original post

2 REPLIES 2

Re: How to get CustomerId in API in magento 2?

Finally got the solution, i am trying my best to explain it.

so here's how Magento works !!

Magento gets the customer data with passing anything except token value in API call.

In between magento calls some API related controllers for the same

vendor/magento/module-customer/etc/webapi.xml

<route url="/V1/customers/me" method="GET">
        <service class="Magento\Customer\Api\CustomerRepositoryInterface" method="getById"/>
        <resources>
            <resource ref="self"/>
        </resources>
        <data>
            <parameter name="customerId" force="true">%customer_id%</parameter>
        </data>
 </route>

in this API call magento get customer data based on token.

Based on API url url="/V1/customers/me" magento calls function from

Magento calls In-between call of dispatch function.

vendor/magento/module-webapi/Controller/Rest.php

 public function dispatch(\Magento\Framework\App\RequestInterface $request)
 {
     /** In between code **/
     // In the same file     $this->processApiRequest();

 }
 protected function processApiRequest()
 {      $inputParams = $this->getInputParamsResolver()->resolve();
 }

And this resolve() function calls override() function

vendor/magento/module-webapi/Controller/Rest/InputParamsResolver.php

 public function resolve()
 {      $inputData = $this->paramsOverrider->override($inputData, $route->getParameters());
 }

and overide() function calls getOverriddenValue() function

vendor/magento/module-webapi/Controller/Rest/ParamsOverrider.php

 public function override(array $inputData, array $parameters)
 {      $value = $this->paramOverriders[$paramValue]->getOverriddenValue();
 }

getOverriddenValue() calls ParamOverriderInterface

vendor/magento/framework/Webapi/Rest/Request/ParamOverriderInterface.php

  • To Override parameter values
  • Parameters in the webapi.xml can be forced. This ensures that on specific routes, a specific value is always used.
  • For instance, if there is a ".../me/..." route, the route should use only user information specific to the
  • currently logged in user. More specifically, if there was a "/customers/me/addresses" route, the service method
  • invoked could have a signature of "getAddresses($customerId)", but in the webapi.xml, the $customerId parameter
  • would be forced to be the customer id of the current authenticated user.
  • The forced override parameter configuration is in the webapi.

   <data>
     <parameter name="customer.id" force="true">%customer_id%</parameter>
   </data>

and for business logic of this function is in file -

vendor/magento/module-webapi/Controller/Rest/ParamOverriderCustomerId.php

   /**
    * {@inheritDoc}
    */
   public function getOverriddenValue()
   {
       if ($this->userContext->getUserType() === UserContextInterface::USER_TYPE_CUSTOMER) {
           return $this->userContext->getUserId();
   }
   return null;
   }

So this is how Magento get Customer Id between API calls !!

Re: How to get CustomerId in API in magento 2?

The solution is good but you should also add the parameter to the service method to have access to it.