2

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.

3
// Code to initiate Object Manager
use Magento\Framework\App\Bootstrap;
include_once('../app/bootstrap.php');

$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();
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();
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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