You are here

protected function InstagramPostBlock::refreshAccessToken in Social Feed 8

Update the access token with a "long-lived" one.

Throws

\EspressoDev\InstagramBasicDisplay\InstagramBasicDisplayException

1 call to InstagramPostBlock::refreshAccessToken()
InstagramPostBlock::build in src/Plugin/Block/InstagramPostBlock.php
Builds and returns the renderable array for this block plugin.

File

src/Plugin/Block/InstagramPostBlock.php, line 213

Class

InstagramPostBlock
Provides a 'InstagramPostBlock' block.

Namespace

Drupal\socialfeed\Plugin\Block

Code

protected function refreshAccessToken() {
  $config = $this->config;

  // 50 Days.
  $days_later = 50 * 24 * 60 * 60;

  // Exit if the token doesn't need updating.
  if (empty($config
    ->get('access_token_date')) || $config
    ->get('access_token_date') + $days_later > time()) {
    return;
  }

  // Update the token.
  $instagram = new InstagramBasicDisplay([
    'appId' => $config
      ->get('client_id'),
    'appSecret' => $config
      ->get('app_secret'),
    'redirectUri' => $this->currentRequest
      ->getSchemeAndHttpHost() . Url::fromRoute('socialfeed.instagram_auth')
      ->toString(),
  ]);

  // Refresh this token.
  $token = $instagram
    ->refreshToken($config
    ->get('access_token'), TRUE);
  if ($token) {
    $config
      ->set('access_token', $token);
    $config
      ->set('access_token_date', time());
    $config
      ->save();
  }
}