You are here

function feeds_youtube_googleapi_youtube_client_factory in Feeds: YouTube Parser 7.3

Uility function to create Google Youtube API client.

Return value

Google_Client Google Youtube API client

3 calls to feeds_youtube_googleapi_youtube_client_factory()
FeedsYoutubeParser::parse in plugins/FeedsYoutubeParser.inc
Parse the extra mapping sources provided by this parser.
feeds_youtube_authenticate in includes/admin.inc
Custom menu callback to handle Google API authentication process.
feeds_youtube_revoke_authentication in includes/admin.inc
Custom menu callback to revoke Google API access token.

File

./feeds_youtube.module, line 125
Adds a YouTube feed processor to the Feeds module.

Code

function feeds_youtube_googleapi_youtube_client_factory() {
  $libraries = libraries_load('google-api-php-client');
  if (!$libraries['loaded']) {
    drupal_set_message(t('Google API library is not installed. Please refer to README.txt for more information about the installation'), 'error');
    drupal_goto('admin/structure/feeds/google-api-oauth');
  }
  $feeds_youtube_api_settings = variable_get('feeds_youtube_api_settings', array());
  $oauth2_client_id = variable_get('google_oauth_client_id', '');
  $oauth2_client_secret = variable_get('google_oauth_client_secret', '');
  $developer_key = variable_get('google_oauth_api_key', '');
  if (!empty($oauth2_client_id) && !empty($oauth2_client_secret) && !empty($developer_key)) {
    $client = new Google_Client();
    $client
      ->setClientId($oauth2_client_id);
    $client
      ->setClientSecret($oauth2_client_secret);
    $client
      ->setApprovalPrompt('force');
    $client
      ->setAccessType('offline');
    $client
      ->setDeveloperKey($developer_key);
    $client
      ->setScopes('https://www.googleapis.com/auth/youtube');
    $redirect = filter_var(url(current_path(), array(
      'absolute' => TRUE,
    )), FILTER_SANITIZE_URL);
    $client
      ->setRedirectUri($redirect);
    if (!empty($feeds_youtube_api_settings['token'])) {
      $client
        ->setAccessToken($feeds_youtube_api_settings['token']);
      if ($client
        ->isAccessTokenExpired()) {
        $client
          ->refreshToken($client
          ->getRefreshToken());
        $feeds_youtube_api_settings['token'] = $client
          ->getAccessToken();
        variable_get('feeds_youtube_api_settings', $feeds_youtube_api_settings);
      }
    }
    return $client;
  }
  else {
    drupal_set_message(t('Google APIs access is not configured.'), 'error');
    drupal_goto('admin/structure/feeds/google-api-oauth');
  }
}