{"id":108952,"date":"2018-01-15T07:09:33","date_gmt":"2018-01-15T07:09:33","guid":{"rendered":"https:\/\/webkul.com\/blog\/?p=108952"},"modified":"2026-03-11T13:24:27","modified_gmt":"2026-03-11T13:24:27","slug":"get-access-token-logged-customer-magento2","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/get-access-token-logged-customer-magento2\/","title":{"rendered":"How to get Access Token of logged in customer in Magento 2"},"content":{"rendered":"\n<p>Here, we will explain that how to get access token of logged in customer. In Authorisation process of <a href=\"https:\/\/webkul.com\/api\/\">Rest Api<\/a> we need to provide access token.<\/p>\n\n\n\n<p>We can get access token using following code. I assume that i have already installed demo <a href=\"https:\/\/store.webkul.com\/Magento-2.html\">Magento 2 module<\/a>. I will create only controller file. In which adding\u00a0 code for getting access token.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?php\n\nnamespace Test\\Module\\Controller\\Test;\n\nuse Magento\\Customer\\Model\\Session;\nuse Magento\\Framework\\App\\Action\\Context;\n\nclass Token extends \\Magento\\Customer\\Controller\\AbstractAccount\n{\n    \/**\n    * @var \\Magento\\Customer\\Model\\Session\n    *\/\n    protected $_customerSession;\n\n    \/**\n    * @param Context          $context\n    * @param Session          $customerSession\n    * @SuppressWarnings(PHPMD.ExcessiveParameterList)\n    *\/\n    public function __construct(\n        Context $context,\n        Session $customerSession,\n        \\Magento\\Integration\\Model\\Oauth\\TokenFactory $tokenModelFactory\n    ) {\n        $this-&gt;_customerSession = $customerSession;\n        $this-&gt;_tokenModelFactory = $tokenModelFactory;\n        parent::__construct(\n            $context\n        );\n    }\n\n    public function execute()\n    {\n        $customerId = $this-&gt;_customerSession-&gt;getCustomer()-&gt;getId();\n        $customerToken = $this-&gt;_tokenModelFactory-&gt;create();\n        echo &quot;Customer-token=&gt; &quot;.$tokenKey = $customerToken-&gt;createCustomerToken($customerId)-&gt;getToken();\n    }\n}<\/pre>\n\n\n\n<p>You can check in execute function. First line we get logged in customer id from customer session. In second line we create object for&nbsp;<em><strong>\\Magento\\Integration\\Model\\Oauth\\TokenFactory<\/strong><\/em> .<\/p>\n\n\n\n<p>Using this object create customer token by calling <strong><em>createCustomerToken<\/em><\/strong> function. Now you can run this controller and get customer access token.<\/p>\n\n\n\n<p>I hope this blog will help you. If any query then you can comment below. Thanks<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here, we will explain that how to get access token of logged in customer. In Authorisation process of Rest Api we need to provide access token. We can get access token using following code. I assume that i have already installed demo Magento 2 module. I will create only controller file. In which adding\u00a0 code <a href=\"https:\/\/webkul.com\/blog\/get-access-token-logged-customer-magento2\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":115,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[302],"tags":[6066,2070],"class_list":["post-108952","post","type-post","status-publish","format-standard","hentry","category-magento2","tag-get-access-token-of-logged-in-customer","tag-magento2"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>how to get access token of logged in customer in magento2<\/title>\n<meta name=\"description\" content=\"get access token of logged in customer in magento2.Using this you can call rest api for customer.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webkul.com\/blog\/get-access-token-logged-customer-magento2\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"how to get access token of logged in customer in magento2\" \/>\n<meta property=\"og:description\" content=\"get access token of logged in customer in magento2.Using this you can call rest api for customer.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/get-access-token-logged-customer-magento2\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-01-15T07:09:33+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-11T13:24:27+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Shubham Sharma\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Shubham Sharma\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/get-access-token-logged-customer-magento2\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/get-access-token-logged-customer-magento2\/\"},\"author\":{\"name\":\"Shubham Sharma\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/ae41bc19a6783d2f09c6b9b3a0fbddfd\"},\"headline\":\"How to get Access Token of logged in customer in Magento 2\",\"datePublished\":\"2018-01-15T07:09:33+00:00\",\"dateModified\":\"2026-03-11T13:24:27+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/get-access-token-logged-customer-magento2\/\"},\"wordCount\":136,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"keywords\":[\"get access token of logged in customer\",\"Magento2\"],\"articleSection\":[\"Magento2\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/get-access-token-logged-customer-magento2\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/get-access-token-logged-customer-magento2\/\",\"url\":\"https:\/\/webkul.com\/blog\/get-access-token-logged-customer-magento2\/\",\"name\":\"how to get access token of logged in customer in magento2\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"datePublished\":\"2018-01-15T07:09:33+00:00\",\"dateModified\":\"2026-03-11T13:24:27+00:00\",\"description\":\"get access token of logged in customer in magento2.Using this you can call rest api for customer.\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/get-access-token-logged-customer-magento2\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/get-access-token-logged-customer-magento2\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/get-access-token-logged-customer-magento2\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to get Access Token of logged in customer in Magento 2\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/ae41bc19a6783d2f09c6b9b3a0fbddfd\",\"name\":\"Shubham Sharma\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/cdf13545eee5ced4cecd7bd6cb94c1d842ec000d359f91dd900e0feec6242c3b?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/cdf13545eee5ced4cecd7bd6cb94c1d842ec000d359f91dd900e0feec6242c3b?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"caption\":\"Shubham Sharma\"},\"url\":\"https:\/\/webkul.com\/blog\/author\/shubham-sharma967\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"how to get access token of logged in customer in magento2","description":"get access token of logged in customer in magento2.Using this you can call rest api for customer.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/webkul.com\/blog\/get-access-token-logged-customer-magento2\/","og_locale":"en_US","og_type":"article","og_title":"how to get access token of logged in customer in magento2","og_description":"get access token of logged in customer in magento2.Using this you can call rest api for customer.","og_url":"https:\/\/webkul.com\/blog\/get-access-token-logged-customer-magento2\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2018-01-15T07:09:33+00:00","article_modified_time":"2026-03-11T13:24:27+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png","type":"image\/png"}],"author":"Shubham Sharma","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Shubham Sharma","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/get-access-token-logged-customer-magento2\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/get-access-token-logged-customer-magento2\/"},"author":{"name":"Shubham Sharma","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/ae41bc19a6783d2f09c6b9b3a0fbddfd"},"headline":"How to get Access Token of logged in customer in Magento 2","datePublished":"2018-01-15T07:09:33+00:00","dateModified":"2026-03-11T13:24:27+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/get-access-token-logged-customer-magento2\/"},"wordCount":136,"commentCount":1,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"keywords":["get access token of logged in customer","Magento2"],"articleSection":["Magento2"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/get-access-token-logged-customer-magento2\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/get-access-token-logged-customer-magento2\/","url":"https:\/\/webkul.com\/blog\/get-access-token-logged-customer-magento2\/","name":"how to get access token of logged in customer in magento2","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"datePublished":"2018-01-15T07:09:33+00:00","dateModified":"2026-03-11T13:24:27+00:00","description":"get access token of logged in customer in magento2.Using this you can call rest api for customer.","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/get-access-token-logged-customer-magento2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/get-access-token-logged-customer-magento2\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/get-access-token-logged-customer-magento2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to get Access Token of logged in customer in Magento 2"}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/ae41bc19a6783d2f09c6b9b3a0fbddfd","name":"Shubham Sharma","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/cdf13545eee5ced4cecd7bd6cb94c1d842ec000d359f91dd900e0feec6242c3b?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/cdf13545eee5ced4cecd7bd6cb94c1d842ec000d359f91dd900e0feec6242c3b?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","caption":"Shubham Sharma"},"url":"https:\/\/webkul.com\/blog\/author\/shubham-sharma967\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/108952","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/115"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=108952"}],"version-history":[{"count":6,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/108952\/revisions"}],"predecessor-version":[{"id":530102,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/108952\/revisions\/530102"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=108952"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=108952"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=108952"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}