0

I try to create programmatically an integration in Magento 2.

I follow this tutorial :

https://magecomp.com/blog/create-activate-authorize-a-new-integration-programmatically-in-magento-2/

I have something like that :

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

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

// Add grant permission
$this->authorizationService->grantAllPermissions($integrationId);

// Activate and Authorize
$token = $this->tokenFactory->create();
$token->createVerifierToken($consumerId);
$token->setType('access');
$token->save();

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?

5
  • 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

0

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:

Magento\Integration\Model\ResourceModel\Oauth\Token->save($myToken)

1
  • 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, privacy policy and cookie policy

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