You are here

function feeds_youtube_authenticate in Feeds: YouTube Parser 7.3

Custom menu callback to handle Google API authentication process.

Return value

string HTML link to authenticate user on Google API.

1 string reference to 'feeds_youtube_authenticate'
feeds_youtube_menu in ./feeds_youtube.module
Implements hook_menu().

File

includes/admin.inc, line 43
admin functionality implementation.

Code

function feeds_youtube_authenticate() {
  $client = feeds_youtube_googleapi_youtube_client_factory();
  $feeds_youtube_api_settings = variable_get('feeds_youtube_api_settings', array());
  if (isset($_GET['code'])) {
    if (strval($feeds_youtube_api_settings['state']) !== strval($_GET['state'])) {
      die('The session state did not match.');
    }
    $client
      ->authenticate($_GET['code']);
    $feeds_youtube_api_settings['token'] = $client
      ->getAccessToken();
    $redirect = filter_var(url(current_path(), array(
      'absolute' => TRUE,
    )), FILTER_SANITIZE_URL);
    header('Location: ' . $redirect);
  }
  if ($client
    ->getAccessToken() && !$client
    ->isAccessTokenExpired()) {
    $client
      ->revokeToken($feeds_youtube_api_settings['token']);
    variable_set('feeds_youtube_api_settings', $feeds_youtube_api_settings);
    drupal_set_message(t('You have successfully authenticated your website to use Google API.'));
    return l(t('Revoke Google API access'), 'admin/structure/feeds/revoke-google-api-authentication', array(
      'attributes' => array(
        'class' => array(
          'button',
        ),
      ),
    ));
  }
  $state = mt_rand();
  $client
    ->setState($state);
  $feeds_youtube_api_settings['state'] = $state;
  $auth_url = $client
    ->createAuthUrl();
  variable_set('feeds_youtube_api_settings', $feeds_youtube_api_settings);
  return l(t('Authenticate Google account'), $auth_url, array(
    'attributes' => array(
      'class' => array(
        'button',
      ),
    ),
  ));
}