While creating a module i want to create the integration for accessing the API's. I have found two ways to do that in
The first way is pretty straight forward to use and I know it will not be rejected by Magento Marketplace.
$this->integrationManager->processIntegrationConfig([Constants::INTEGRATION_NAME]);
The above code is added in the InstallData.php and the integration is created but the problem is that the Magento Admin have to manually do the activation by going to the Integration page and click activate. After click on the activate the endpoint is then able to generate the indefinite access_token.
I wanted to skip this step so that there is no such additional step. So, i found the other way.
The second way is to create the integration and activate there and then in the InstallData.php. Here is the code snippet:
$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();
return $uri;
} catch (Exception $e) {
$this->_logger->debug('Error creating token :: ' . $e->getMessage());
return null;
}
}
Using the second way i am able to get the access_token and then i can share this token to external application which can then use this access_token to fetch data. I tried that and it's working. Please note that i can use dependency injection in the constructor to not make use of object manager. But the point remains the same.
The problem is now that I want to know if using this code the Marketplace would reject my extension for publishing? If this is allowed are there any other things that I need to do?