12 use Magento\Integration\Model\Oauth\ConsumerFactory;
14 use Magento\Integration\Model\Oauth\TokenFactory as TokenFactory;
84 ConsumerFactory $consumerFactory,
85 TokenFactory $tokenFactory,
86 IntegrationOauthHelper $dataHelper,
87 \
Magento\Framework\HTTP\ZendClient $httpClient,
88 \Psr\Log\LoggerInterface
$logger,
90 TokenProvider $tokenProvider
93 $this->_consumerFactory = $consumerFactory;
94 $this->_tokenFactory = $tokenFactory;
95 $this->_dataHelper = $dataHelper;
96 $this->_httpClient = $httpClient;
98 $this->_oauthHelper = $oauthHelper;
99 $this->_tokenProvider = $tokenProvider;
109 private function getDateHelper()
111 if ($this->_dateHelper ===
null) {
113 ->get(\
Magento\Framework\Stdlib\DateTime\DateTime::class);
115 return $this->_dateHelper;
124 $consumerData[
'key'] = $this->_oauthHelper->generateConsumerKey();
125 $consumerData[
'secret'] = $this->_oauthHelper->generateConsumerSecret();
126 $consumer = $this->_consumerFactory->create()->setData($consumerData);
131 }
catch (\Exception $exception) {
132 throw new \Magento\Framework\Oauth\Exception(
134 "The oAuth consumer account couldn't be created due to an unexpected error. Please try again later." 146 $consumer = $this->_consumerFactory->create()->load($consumerId);
147 $existingToken = $this->_tokenProvider->getIntegrationTokenByConsumerId($consumer->getId());
148 if ($existingToken && $clearExistingToken) {
149 $existingToken->delete();
150 unset($existingToken);
152 }
catch (\Exception $e) {
154 if (!isset($existingToken)) {
155 $consumer = $this->_consumerFactory->create()->load($consumerId);
156 $this->_tokenFactory->create()->createVerifierToken($consumerId);
157 $this->_tokenProvider->createRequestToken($consumer);
158 $this->_tokenProvider->getAccessToken($consumer);
170 $consumer = $this->_consumerFactory->create()->load($consumerId);
171 $token = $this->_tokenProvider->getIntegrationTokenByConsumerId($consumer->getId());
172 if (
$token->getType() != OauthTokenModel::TYPE_ACCESS) {
175 }
catch (\Exception $e) {
187 return $this->_consumerFactory->create()->load($consumerId);
190 }
catch (\Exception $exception) {
191 throw new \Magento\Framework\Oauth\Exception(
192 __(
"The oAuth consumer account couldn't be loaded due to an unexpected error. Please try again later.")
203 return $this->_consumerFactory->create()->load($key,
'key');
206 }
catch (\Exception $exception) {
207 throw new \Magento\Framework\Oauth\Exception(
208 __(
"The oAuth consumer account couldn't be loaded due to an unexpected error. Please try again later.")
220 $consumer->setUpdatedAt($this->getDateHelper()->gmtDate());
222 if (!$consumer->getId()) {
223 throw new \Magento\Framework\Oauth\Exception(
224 __(
'A consumer with "%1" ID doesn\'t exist. Verify the ID and try again.', $consumerId)
227 $consumerData = $consumer->getData();
228 $verifier = $this->_tokenFactory->create()->createVerifierToken($consumerId);
229 $storeBaseUrl = $this->_storeManager->getStore()->getBaseUrl();
230 $this->_httpClient->setUri($endpointUrl);
231 $this->_httpClient->setParameterPost(
233 'oauth_consumer_key' => $consumerData[
'key'],
234 'oauth_consumer_secret' => $consumerData[
'secret'],
235 'store_base_url' => $storeBaseUrl,
236 'oauth_verifier' => $verifier->getVerifier(),
239 $maxredirects = $this->_dataHelper->getConsumerPostMaxRedirects();
240 $timeout = $this->_dataHelper->getConsumerPostTimeout();
241 $this->_httpClient->setConfig([
'maxredirects' => $maxredirects,
'timeout' => $timeout]);
242 $this->_httpClient->request(\
Magento\Framework\HTTP\ZendClient::POST);
243 return $verifier->getVerifier();
246 }
catch (\
Magento\Framework\Oauth\Exception $exception) {
248 }
catch (\Exception $exception) {
249 $this->_logger->critical($exception);
250 throw new \Magento\Framework\Oauth\Exception(
251 __(
'The attempt to post data to consumer failed due to an unexpected error. Please try again later.')
262 $data = $consumer->getData();
273 $consumer = $this->_consumerFactory->create()->load($consumerId);
274 $existingToken = $this->_tokenProvider->getIntegrationTokenByConsumerId($consumer->getId());
275 $existingToken->delete();
277 }
catch (\Exception $e) {
291 $consumer = $this->_consumerFactory->create()->load($consumerId);
292 if (!$consumer->getId()) {
294 __(
'A consumer with ID "%1" doesn\'t exist. Verify the ID and try again.', $consumerId)
getAccessToken($consumerId)
loadConsumer($consumerId)
__construct(\Magento\Store\Model\StoreManagerInterface $storeManager, ConsumerFactory $consumerFactory, TokenFactory $tokenFactory, IntegrationOauthHelper $dataHelper, \Magento\Framework\HTTP\ZendClient $httpClient, \Psr\Log\LoggerInterface $logger, OauthHelper $oauthHelper, TokenProvider $tokenProvider)
_loadConsumerById($consumerId)
createConsumer($consumerData)
deleteConsumer($consumerId)
createAccessToken($consumerId, $clearExistingToken=false)
postToConsumer($consumerId, $endpointUrl)
deleteIntegrationToken($consumerId)