I try to create programmatically an integration in Magento 2.

I follow this tutorial :


I have something like that :

// Create Integration
$integrationFactory = $this->integrationFactory->create();
$integration = $integrationFactory->setData($integrationData);
$integrationId = $integration->getId();
$consumerName = 'Integration' . $integrationId;

// Create consumer
$consumer = $this->oauthService->createConsumer(['name' => $consumerName]);
$consumerId = $consumer->getId();

// Add grant permission

// Activate and Authorize
$token = $this->tokenFactory->create();

It's work but all 'save' function are deprecated. I don't succeed to find another way to save integration, consumer and token.

Do anyone have any idea?

  • What's the type of $this->tokenFactory? Magento used Service Contracts for saving / loading entities, so you should look for something named TokenRepository instead of using the explicit model. Oct 7, 2019 at 8:39
  • You have to use Service Contract (the best way), or ResourceModel
    – PЯINCƎ
    Oct 7, 2019 at 8:58
  • @simonthesorcerer : $this->tokenFactory type is Magento\Integration\Model\Oauth\Token
    – Kozame
    Oct 7, 2019 at 9:03
  • @PЯINCƏ Do you have any examples ?
    – Kozame
    Oct 7, 2019 at 9:04
  • @Kozame siphor.com/add-a-magento-2-service-contract
    – PЯINCƎ
    Oct 7, 2019 at 9:18

1 Answer 1


I couldn't find a Service Contract (maybe depends on the Magento version, they are still in development iirc), so the best way should be using the resource model as pointed out in the comments:


  • I use Magento\Integration\Model\ResourceModel\Integration and Magento\Integration\Model\ResourceModel\Oauth\Token : work well !
    – Kozame
    Oct 7, 2019 at 12:21

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.