I have to create, activate and authorize a 'New Integration' programmatically. And then return it's 'Access Token'. I followed the guide: http://devdocs.magento.com/guides/v2.0/howdoi/webapi/integration.html but unable to solve.

1 Answer 1

// Code to initiate Object Manager
use Magento\Framework\App\Bootstrap;

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

//Set your Data
$name = 'Your-Integration-Name';
$email = 'Set-Your-EmailId';
$endpoint = 'Set-Your-Url';

// Code to check whether the Integration is already present or not
$integrationExists = $objectManager->get('Magento\Integration\Model\IntegrationFactory')->create()->load($name,'name')->getData();
    $integrationData = array(
        'name' => $name,
        'email' => $email,
        'status' => '1',
        'endpoint' => $endpoint,
        'setup_type' => '0'
        // Code to create Integration
        $integrationFactory = $objectManager->get('Magento\Integration\Model\IntegrationFactory')->create();
        $integration = $integrationFactory->setData($integrationData);
        $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();

        // Code to grant permission
        $authrizeService = $objectManager->get('Magento\Integration\Model\AuthorizationService');

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.