InstagramAuthController.php in Social Feed 8
File
src/Controller/InstagramAuthController.php
View source
<?php
namespace Drupal\socialfeed\Controller;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Url;
use EspressoDev\InstagramBasicDisplay\InstagramBasicDisplay;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
class InstagramAuthController extends ControllerBase {
protected $currentRequest;
public function __construct(ConfigFactoryInterface $config_factory, Request $request) {
$this->configFactory = $config_factory;
$this->currentRequest = $request;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('request_stack')
->getCurrentRequest());
}
public function accessToken() {
$code = $this->currentRequest->query
->get('code');
$message = 'Something went wrong. The access token could not be created.';
$token = '';
if ($code) {
$config = $this->configFactory
->getEditable('socialfeed.instagramsettings');
$instagram = new InstagramBasicDisplay([
'appId' => $config
->get('client_id'),
'appSecret' => $config
->get('app_secret'),
'redirectUri' => Url::fromRoute('socialfeed.instagram_auth', [], [
'absolute' => TRUE,
])
->toString(),
]);
$token = $instagram
->getOAuthToken($code, TRUE);
if ($token) {
$token = $instagram
->getLongLivedToken($token, TRUE);
$config
->set('access_token', $token);
$config
->set('access_token_date', time());
$config
->save();
$message = 'Your Access Token has been generated and saved.';
}
}
$build = [
'#markup' => $this
->t($message) . ' ' . $token,
];
return $build;
}
}