adminHelper = $backendData; $this->config = $config; $this->scopeConfig = $scopeConfigInterface; $this->storeManager = $storeManager; $this->adminUser = $adminUser; $this->userResource = $userResource; $this->helper = $helper; $this->resultPageFactory = $resultPageFactory; parent::__construct($context); } /** * Execute method. * * @return null */ public function execute() { $code = $this->getRequest()->getParam('code', false); $userId = $this->getRequest()->getParam('state'); //load admin user $adminUser = $this->adminUser->create(); $this->userResource->load($adminUser, $userId); //app code and admin user must be present if ($code && $adminUser->getId()) { $clientId = $this->scopeConfig->getValue( \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_CLIENT_ID ); $clientSecret = $this->scopeConfig->getValue( \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_CLIENT_SECRET_ID ); //callback uri if not set custom $redirectUri = $this->storeManager->getStore() ->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB, true); $redirectUri .= 'connector/email/callback'; $data = 'client_id=' . $clientId . '&client_secret=' . $clientSecret . '&redirect_uri=' . $redirectUri . '&grant_type=authorization_code' . '&code=' . $code; //callback url $url = $this->config->getTokenUrl(); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, 60); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/x-www-form-urlencoded']); $response = json_decode(curl_exec($ch)); if ($response === false) { $this->helper->error('Error Number: ' . curl_errno($ch), []); } if (isset($response->error)) { $this->helper->error('OAUTH failed ' . $response->error, []); } elseif (isset($response->refresh_token)) { //save the refresh token to the admin user $adminUser->setRefreshToken( $this->helper->encryptor->encrypt($response->refresh_token) ); $this->userResource->save($adminUser); } //redirect to automation index page return $this->_redirect($this->adminHelper->getUrl('dotdigitalgroup_email/studio')); } return $this->resultPageFactory->create() ->setStatusHeader(404, '1.1', 'Not Found') ->setHeader('Status', '404 File not found'); } }