{"id":894,"date":"2020-03-21T07:58:41","date_gmt":"2020-03-21T07:58:41","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2020\/03\/21\/get-access-token-of-logged-in-customer-in-magento-2\/"},"modified":"2025-05-22T14:42:05","modified_gmt":"2025-05-22T09:12:05","slug":"get-access-token-of-logged-in-customer-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/get-access-token-of-logged-in-customer-in-magento-2\/","title":{"rendered":"How To Get Access Token Of Logged In Customer In Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The post shows the programmatic method to&nbsp;<em><strong>get access token of logged in customer in Magento 2<\/strong><\/em>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can use this method when, for example, you want to improve the login method for customers and allowing them to login via Facebook or Google.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The developer must give the access token to make a web&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/magento-2-api\/\" target=\"_blank\" rel=\"noreferrer noopener\">Magento 2 API<\/a>&nbsp;call from a client such as a mobile application. This token serves as an electronic key to access the API.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to get access token of logged in customer in Magento 2:<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">use Magento\\Customer\\Model\\Session;\nuse Magento\\Framework\\App\\Action\\Context;\nuse Magento\\Integration\\Model\\Oauth\\TokenFactory;\n\nprotected $customerSession;\nprotected $tokenModelFactory;\n \n \npublic function __construct(\n        Session $customerSession,\n        TokenFactory $tokenModelFactory\n) {\n    $this->customerSession = $customerSession;\n    $this->tokenModelFactory = $tokenModelFactory;\n        \n} \n\npublic function getToken(){\n    $customerId = $this->customerSession->getCustomer()->getId();\n    $customerToken = $this->tokenModelFactory->create();\n    return $customerToken->createCustomerToken($customerId)->getToken();\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Apart from the programmatic solution, you can also use&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/magento-2-api-get-customer-token\/\" target=\"_blank\" rel=\"noreferrer noopener\">Magento 2 API to get customer token<\/a>&nbsp;by calling a GET request with credentials.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Feel free to share the solution with fellow developers via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The post shows the programmatic method to&nbsp;get access token of logged in customer in Magento 2. You can use this method when, for example, you want to improve the login method for customers and allowing them to login via Facebook or Google. The developer must give the access token to make a web&nbsp;Magento 2 API&nbsp;call [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[34],"tags":[],"class_list":["post-894","post","type-post","status-publish","format-standard","hentry","category-magento"],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/894","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=894"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/894\/revisions"}],"predecessor-version":[{"id":15018,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/894\/revisions\/15018"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=894"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=894"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=894"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}