How to Create, Activate and Authorize a ‘New Integration’ Programmatically in Magento 2

img How to Create Activate and Authorize a New Integration Programmatically in Magento 2

Third party API integration in Magento 2 is used to get benefitted with features and functionality that default Magento doesn’t provide. With the 3rd party API integrations, store admin can automate work to introduce flexibility and new perspectives. In Magento 2, API integrations is used to embed functionality and features from a system to guarantee smooth information delivery and integrated user experience.

An integration pays a vital role in calling Magento 2 web APIs to utilize third party services. Out of the box, Magento 2 APIs support many third-party systems like Analysis tracker, CRMs, ERP systems, order processing systems, PIMs, marketing automations and accounting. The integration is a bridge that communicate with the Magento systems to use web services.

To user 3rd party services in Magento 2, you need to create integration from Magento 2 backend, activate the integration object and use tokens to request access. All this can be done through Magento 2 backend which a bit time consuming task. Here, we have come up with custom code to create, activate and authorize a new integration programmatically in Magento 2.

use Magento\Framework\App\Bootstrap;
include_once('../app/bootstrap.php');

$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();

//Set your Data
$name = ‘Integration-Name';
$email = 'Set- EmailId';
$endpoint = 'Set- Url'; (e.g 'http://localhost/magento/')

// Code to check whether the Integration is already present or not
$integrationExists = $objectManager->get('Magento\Integration\Model\IntegrationFactory')->create()->load($name,'name')->getData();
if(empty($integrationExists)){
    $integrationData = array(
        'name' => $name,
        'email' => $email,
        'status' => '1',
        'endpoint' => $endpoint,
        'setup_type' => '0'
    );
    try{
        // Code to create Integration
        $integrationFactory = $objectManager->get('Magento\Integration\Model\IntegrationFactory')->create();
        $integration = $integrationFactory->setData($integrationData);
        $integration->save();
        $integrationId = $integration->getId();$consumerName = 'Integration' . $integrationId;


        // Code to create consumer
        $oauthService = $objectManager->get('Magento\Integration\Model\OauthService');
        $consumer = $oauthService->createConsumer(['name' => $consumerName]);
        $consumerId = $consumer->getId();
        $integration->setConsumerId($consumer->getId());
        $integration->save();
        // Code to grant permission
        $authrizeService = $objectManager->get('Magento\Integration\Model\AuthorizationService');
        $authrizeService->grantAllPermissions($integrationId);

        // Code to Activate and Authorize
        $token = $objectManager->get('Magento\Integration\Model\Oauth\Token');
        $uri = $token->createVerifierToken($consumerId);
        $token->setType('access');
        $token->save();

    }catch(Exception $e){
        echo 'Error : '.$e->getMessage();
    }
}

Simply customize and use above code to easily and quickly create new integration in Magento 2 and start using 3rd party web services seamlessly. Hope this code has enough helped you to serve the purpose, let me know for which web service, you implemented the code. Also appreciate our efforts of creating programmatic code by smashing 5 stars below. Comment down your questions, feedback and suggestions if any, we would be glad to hear from you.

Previous Article

How to Add Custom Admin Notifications in Magento 2

Next Article

How to Remove Image Magnifier & Slider in Magento 2 Product Page

Write a Comment
  1. Sudhir Yadav

    Is it the right way to do it? When publishing the extension does it pass the quality test?

Leave a Comment

Your email address will not be published. Required fields are marked *

Get Connect With Us

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨