1

I am trying to integrate my external API in my magento 2.4.6 instance.

I Used Guzzlehttp to make and fetch the data and handle the request.

The problem is, I have to provide Username & password to the token endpoint and fetch the Access token.

Later I can provide that token to access further resources.

My question is, I want to store the Access token somewhere, that I dont want to regenerate for each call. Where can I store it?

or else any other mechanisms are expected.

4 Answers 4

0

In Magento 2, you can use the built-in functionality to store and retrieve custom configuration values. This is a good place to store your API access token. Here's how you can do it:

  1. Create a new system.xml file in your module to add a new field in the Magento admin panel where you can input your API token.
<!-- File: app/code/Vendor/Module/etc/adminhtml/system.xml --> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd"> <system> <section id="your_section"> <group id="your_group"> <field id="api_token" translate="label" type="obscure" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1"> <label>API Token</label> <backend_model>Magento\Config\Model\Config\Backend\Encrypted</backend_model> </field> </group> </section> </system> </config> 
  1. Now you can set the API token in the Magento admin panel under Stores > Configuration > Your Section > Your Group > API Token.

  2. To retrieve the API token in your code, you can use the ScopeConfigInterface:


 <?php namespace Vendor\Module\Model; class YourClass { protected $scopeConfig; protected $encryptor; public function __construct( \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Framework\Encryption\EncryptorInterface $encryptor ) { $this->scopeConfig = $scopeConfig; $this->encryptor = $encryptor; } public function getApiToken() { $encryptedToken = $this->scopeConfig->getValue( 'your_section/your_group/api_token', \Magento\Store\Model\ScopeInterface::SCOPE_STORE ); return $this->encryptor->decrypt($encryptedToken); } } 

Remember to replace Vendor, Module, your_section, your_group, and YourClass with your actual vendor name, module name, section ID, group ID, and class name.


Also, to set a configuration value programmatically in Magento 2,

you can use the Magento\Framework\App\Config\Storage\WriterInterface and Magento\Framework\App\Config\ScopeConfigInterface classes.

Example:

<?php namespace Vendor\Module\Model; class YourClass { protected $configWriter; public function __construct( \Magento\Framework\App\Config\Storage\WriterInterface $configWriter ) { $this->configWriter = $configWriter; } public function setApiToken($value) { $this->configWriter->save( 'your_section/your_group/api_token', $value, $scope = \Magento\Framework\App\Config\ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeId = 0 ); } } 

The save method takes four parameters:

the path of the configuration value, the value to save, the scope of the configuration value (default, websites, or stores), and the ID of the scope.

5
  • This is ok, But The access token will expire in 12 hrs. I have created username and password fields in system.xml . Using this I can fetch the access token. So it will not be feasible Commented May 30, 2024 at 10:32
  • You can create 1 custom cron which runs every 11 hours (suppose) to update token value in configuration, That cron will fetch tokens and update the configuration value of your token, so every time when you will fetch the token you will get the latest token. Commented May 30, 2024 at 12:32
  • Can i Update the value stored in the configuration via code? Can you share how to update it? Commented May 30, 2024 at 13:08
  • @HaarishInfantRaj, Updated my answer with an example, Please check and approve if it works for you.Thanks! Commented May 30, 2024 at 13:14
  • Also, if you want to explore more, visit store.magenest.com/blog/… Commented May 30, 2024 at 13:16
0

https://www.beehexa.com/devdocs/how-to-create-an-integration-in-magento-2/

use this solution for this, you can use the Magento back-end to create an integration manually or build a custom in Magento 2 extension to create that integration programmatically.

4
  • No!!!! I want to call the external api, Not the magento's api. Commented May 29, 2024 at 14:02
  • then you can store your api under store configuration so you can get token whenever you want Commented May 29, 2024 at 14:14
  • How to do that, Any reference? Commented May 29, 2024 at 14:23
  • webkul.com/blog/… Commented May 29, 2024 at 14:33
0

Steps to Get Store Config Value in Magento 2: Step 1: Create the system.xml file inside the etc folder Path – Vendor/Extension/etc/adminhtml/system.xml

<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd"> <system> <section id="custom" translate="label" type="text" sortOrder="310" showInDefault="1" showInWebsite="1" showInStore="1"> <label>Custom Settings</label> <group id="custom_group_id" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1"> <label>Main label</label> <field id="custom_field_id" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1"> <label>Field Name</label> </field> </group> </section> </system> </config> 

Step 2: Now create Data.php file inside the Helper folder Path – Vendor/Extension/Helper/Data.php

<?php namespace Vendor\Extension\Helper; use Magento\Store\Model\ScopeInterface; use Magento\Framework\App\Helper\AbstractHelper; class Data extends AbstractHelper { const XML_CONFIG_PATH = 'custom/custom_group_id/custom_field_id'; public function getConfig() { $configValue = $this->scopeConfig->getValue(self::XML_CONFIG_PATH,ScopeInterface::SCOPE_STORE); return $configValue; } } 

Note – You can inject the helper class anywhere and use the getConfig() function to get the config value.This way you can Get Store Config Value in Magento 2.

0

@Haarish Infant Raj

Hey,

Simply once you have fetched the token than you can store that token to your custom configuration field (if not than create) (save in core_config_data table) and just fetch token from that, not needed to regenerate in every calls.

Thank You!

1
  • I may want to regenerate. But can i programmatically save the config data? Commented May 30, 2024 at 13:09

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.