How to capture the User ID using magento's customer Token?












5














How do I make a call to that URL that captures the User ID, using Magento customer Token in magento 2 rest API?



How Magento 2 makes calls in-between for the APIs ?










share|improve this question
























  • you can directly pass <parameter name="customer.id" force="true">%customer_id%</parameter>
    – Aditya Shah
    Sep 17 '18 at 15:04






  • 1




    it worked!! gives me customer id based on token. Thanks!
    – Yann Martel
    Sep 17 '18 at 15:14










  • You can mark as right if it's working fine for you, it may help future readers.
    – Aditya Shah
    Sep 17 '18 at 15:15
















5














How do I make a call to that URL that captures the User ID, using Magento customer Token in magento 2 rest API?



How Magento 2 makes calls in-between for the APIs ?










share|improve this question
























  • you can directly pass <parameter name="customer.id" force="true">%customer_id%</parameter>
    – Aditya Shah
    Sep 17 '18 at 15:04






  • 1




    it worked!! gives me customer id based on token. Thanks!
    – Yann Martel
    Sep 17 '18 at 15:14










  • You can mark as right if it's working fine for you, it may help future readers.
    – Aditya Shah
    Sep 17 '18 at 15:15














5












5








5


1





How do I make a call to that URL that captures the User ID, using Magento customer Token in magento 2 rest API?



How Magento 2 makes calls in-between for the APIs ?










share|improve this question















How do I make a call to that URL that captures the User ID, using Magento customer Token in magento 2 rest API?



How Magento 2 makes calls in-between for the APIs ?







magento2 customer api rest rest-api






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited yesterday









Aditya Shah

3,6202834




3,6202834










asked Sep 17 '18 at 15:00









Yann MartelYann Martel

2715




2715












  • you can directly pass <parameter name="customer.id" force="true">%customer_id%</parameter>
    – Aditya Shah
    Sep 17 '18 at 15:04






  • 1




    it worked!! gives me customer id based on token. Thanks!
    – Yann Martel
    Sep 17 '18 at 15:14










  • You can mark as right if it's working fine for you, it may help future readers.
    – Aditya Shah
    Sep 17 '18 at 15:15


















  • you can directly pass <parameter name="customer.id" force="true">%customer_id%</parameter>
    – Aditya Shah
    Sep 17 '18 at 15:04






  • 1




    it worked!! gives me customer id based on token. Thanks!
    – Yann Martel
    Sep 17 '18 at 15:14










  • You can mark as right if it's working fine for you, it may help future readers.
    – Aditya Shah
    Sep 17 '18 at 15:15
















you can directly pass <parameter name="customer.id" force="true">%customer_id%</parameter>
– Aditya Shah
Sep 17 '18 at 15:04




you can directly pass <parameter name="customer.id" force="true">%customer_id%</parameter>
– Aditya Shah
Sep 17 '18 at 15:04




1




1




it worked!! gives me customer id based on token. Thanks!
– Yann Martel
Sep 17 '18 at 15:14




it worked!! gives me customer id based on token. Thanks!
– Yann Martel
Sep 17 '18 at 15:14












You can mark as right if it's working fine for you, it may help future readers.
– Aditya Shah
Sep 17 '18 at 15:15




You can mark as right if it's working fine for you, it may help future readers.
– Aditya Shah
Sep 17 '18 at 15:15










1 Answer
1






active

oldest

votes


















4















How magento 2 make calls in-between for the APIs ?




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="MagentoCustomerApiCustomerRepositoryInterface" 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(MagentoFrameworkAppRequestInterface $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 !!





Another solution



$ch = curl_init('dev.magento2.com/rest/V1/customers/me');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'
));
curl_setopt($ch, CURLOPT_COOKIE, 'PHPSESSID=' . $_COOKIE['PHPSESSID']);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

$json = json_decode($result);
echo $json->id;





share|improve this answer























    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
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f242569%2fhow-to-capture-the-user-id-using-magentos-customer-token%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









    4















    How magento 2 make calls in-between for the APIs ?




    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="MagentoCustomerApiCustomerRepositoryInterface" 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(MagentoFrameworkAppRequestInterface $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 !!





    Another solution



    $ch = curl_init('dev.magento2.com/rest/V1/customers/me');
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json'
    ));
    curl_setopt($ch, CURLOPT_COOKIE, 'PHPSESSID=' . $_COOKIE['PHPSESSID']);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);

    //execute post
    $result = curl_exec($ch);

    //close connection
    curl_close($ch);

    $json = json_decode($result);
    echo $json->id;





    share|improve this answer




























      4















      How magento 2 make calls in-between for the APIs ?




      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="MagentoCustomerApiCustomerRepositoryInterface" 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(MagentoFrameworkAppRequestInterface $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 !!





      Another solution



      $ch = curl_init('dev.magento2.com/rest/V1/customers/me');
      curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch, CURLOPT_HTTPHEADER, array(
      'Content-Type: application/json'
      ));
      curl_setopt($ch, CURLOPT_COOKIE, 'PHPSESSID=' . $_COOKIE['PHPSESSID']);
      curl_setopt($ch, CURLOPT_TIMEOUT, 5);
      curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);

      //execute post
      $result = curl_exec($ch);

      //close connection
      curl_close($ch);

      $json = json_decode($result);
      echo $json->id;





      share|improve this answer


























        4












        4








        4







        How magento 2 make calls in-between for the APIs ?




        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="MagentoCustomerApiCustomerRepositoryInterface" 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(MagentoFrameworkAppRequestInterface $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 !!





        Another solution



        $ch = curl_init('dev.magento2.com/rest/V1/customers/me');
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json'
        ));
        curl_setopt($ch, CURLOPT_COOKIE, 'PHPSESSID=' . $_COOKIE['PHPSESSID']);
        curl_setopt($ch, CURLOPT_TIMEOUT, 5);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);

        //execute post
        $result = curl_exec($ch);

        //close connection
        curl_close($ch);

        $json = json_decode($result);
        echo $json->id;





        share|improve this answer















        How magento 2 make calls in-between for the APIs ?




        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="MagentoCustomerApiCustomerRepositoryInterface" 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(MagentoFrameworkAppRequestInterface $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 !!





        Another solution



        $ch = curl_init('dev.magento2.com/rest/V1/customers/me');
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json'
        ));
        curl_setopt($ch, CURLOPT_COOKIE, 'PHPSESSID=' . $_COOKIE['PHPSESSID']);
        curl_setopt($ch, CURLOPT_TIMEOUT, 5);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);

        //execute post
        $result = curl_exec($ch);

        //close connection
        curl_close($ch);

        $json = json_decode($result);
        echo $json->id;






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Sep 17 '18 at 15:16

























        answered Sep 17 '18 at 15:02









        Aditya ShahAditya Shah

        3,6202834




        3,6202834






























            draft saved

            draft discarded




















































            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f242569%2fhow-to-capture-the-user-id-using-magentos-customer-token%23new-answer', 'question_page');
            }
            );

            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







            Popular posts from this blog

            1300-talet

            1300-talet

            Display a custom attribute below product name in the front-end Magento 1.9.3.8