How to Fetch Bearer Token in Magento 2
Magento 2 supports REST API Integration, which lets you speed up getting, sending, and processing data. It also transfers it to the third-party system. Postman is one of the recommended REST clients which connects with Magento 2 API(Application Programming Interface).
Postman is an API testing tool, an API development environment, and a handy HTTP client for testing websites that simplifies API workflow in testing and development.
The bearer token is a cryptic string, usually generated by the server in response to a login request. The Magento access token must be given on the line to allow a web API request from a client. As a result, the token serves as an electronic key that enables you to enter Magento 2 API.
We have to pass an authorized token in the postman while sending API to Magento requested from the postman for security purposes. What if you have the requirement to validate the condition, like if the token is not there, then the condition must be false?
For example, when we forget the password- we use the facility of ‘forget password’ and reset our old password with the new one. In the backend of this process, a temporary token gets generated and passed in the whole process.
In such kind of scenarios, we’ve to use the below method to fetch bearer token in Magento 2.
Method to Fetch Bearer Token in Magento 2
Use the below code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
public function getAuthToken(){ $token = false; $headers = []; foreach ($_SERVER as $name => $value) { if (substr($name, 0, 5) == 'HTTP_') { $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value; } } $authorizationBearer = ''; if(isset($headers['Authorization'])) { $authorizationBearer = $headers['Authorization']; } else if(isset($headers['authorization'])) { $authorizationBearer = $headers['authorization']; } else { $authorizationBearer = ""; } $authorizationBearerArr = explode(' ', $authorizationBearer); if( isset($authorizationBearerArr[0]) && trim($authorizationBearerArr[0]) == 'Bearer' && isset($authorizationBearerArr[1]) ){ $token = $authorizationBearerArr[1]; } return $token; } |
Note: You can use this method for any REST API , postman is used here for example.
I’ve created a preference and called model.
I’ve used the above code in the model file.
You can call the file according to your requirement according the path you’ve passed in the xml file.
For more details you can refer the post: How to Create Custom Rest API in Magento 2
That’s all!
If you have any doubts regarding this post, just mention them in the Comments section below.
I would be happy to help.
Feel free to share the solution with Magento Community via social media.
Thank You.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
Prev
How to Get Product Custom Options Value From Cart and Order in Magento 2
How to Change Database Name in Magento 2
Next