protected function syncPlaylistIds(Google_Service_YouTube $youtube, $remoteId, array $playlistIds)
 {
     $playlistsResponseList = $youtube->playlists->listPlaylists('id,snippet', array('mine' => true));
     foreach ($playlistsResponseList->getItems() as $playlist) {
         $playlistId = $playlist['id'];
         if (!in_array($playlistId, $playlistIds)) {
             $playlistsItemsListResponse = $youtube->playlistItems->listPlaylistItems('snippet', array('playlistId' => $playlistId, 'videoId' => $remoteId));
             foreach ($playlistsItemsListResponse->getItems() as $playlistItem) {
                 $youtube->playlistItems->delete($playlistItem['id']);
             }
         }
     }
     foreach ($playlistIds as $playlistId) {
         if (!$playlistId) {
             continue;
         }
         $playlistsItemsListResponse = $youtube->playlistItems->listPlaylistItems('snippet', array('playlistId' => $playlistId, 'videoId' => $remoteId));
         if (count($playlistsItemsListResponse->getItems())) {
             continue;
         }
         $resourceId = new Google_Service_YouTube_ResourceId();
         $resourceId->setKind('youtube#video');
         $resourceId->setVideoId($remoteId);
         $snippet = new Google_Service_YouTube_PlaylistItemSnippet();
         $snippet->setPlaylistId($playlistId);
         $snippet->setResourceId($resourceId);
         $playlistItem = new Google_Service_YouTube_PlaylistItem();
         $playlistItem->setSnippet($snippet);
         $youtube->playlistItems->insert('snippet', $playlistItem);
     }
 }
 $playlistSnippet->setDescription('A private playlist created with the YouTube API v3');
 // 2. Define the playlist's status.
 $playlistStatus = new Google_Service_YouTube_PlaylistStatus();
 $playlistStatus->setPrivacyStatus('private');
 // 3. Define a playlist resource and associate the snippet and status
 // defined above with that resource.
 $youTubePlaylist = new Google_Service_YouTube_Playlist();
 $youTubePlaylist->setSnippet($playlistSnippet);
 $youTubePlaylist->setStatus($playlistStatus);
 // 4. Call the playlists.insert method to create the playlist. The API
 // response will contain information about the new playlist.
 $playlistResponse = $youtube->playlists->insert('snippet,status', $youTubePlaylist, array());
 $playlistId = $playlistResponse['id'];
 // 5. Add a video to the playlist. First, define the resource being added
 // to the playlist by setting its video ID and kind.
 $resourceId = new Google_Service_YouTube_ResourceId();
 $resourceId->setVideoId('SZj6rAYkYOg');
 $resourceId->setKind('youtube#video');
 // Then define a snippet for the playlist item. Set the playlist item's
 // title if you want to display a different value than the title of the
 // video being added. Add the resource ID and the playlist ID retrieved
 // in step 4 to the snippet as well.
 $playlistItemSnippet = new Google_Service_YouTube_PlaylistItemSnippet();
 $playlistItemSnippet->setTitle('First video in the test playlist');
 $playlistItemSnippet->setPlaylistId($playlistId);
 $playlistItemSnippet->setResourceId($resourceId);
 // Finally, create a playlistItem resource and add the snippet to the
 // resource, then call the playlistItems.insert method to add the playlist
 // item.
 $playlistItem = new Google_Service_YouTube_PlaylistItem();
 $playlistItem->setSnippet($playlistItemSnippet);
 function addChannelToSubscriptions($channel_id)
 {
     // include the required php files - containers client_id and client_secret
     include_once YT4WP_PATH . 'lib/google_api_wrapper_clientid_clientsecret.php';
     try {
         $resourceId = new Google_Service_YouTube_ResourceId();
         $resourceId->setChannelId($channel_id);
         $resourceId->setKind('youtube#channel');
         $subscriptionSnippet = new Google_Service_YouTube_SubscriptionSnippet();
         $subscriptionSnippet->setResourceId($resourceId);
         $subscription = new Google_Service_YouTube_Subscription();
         $subscription->setSnippet($subscriptionSnippet);
         $subscriptionResponse = $youtube->subscriptions->insert('id,snippet', $subscription, array());
         if ($subscriptionResponse) {
             // ajax to return our unsubscribe ID (which differs from the subscribe ID)
             // Check if the user is subscribed to this channel
             $is_user_subscribed = $youtube->subscriptions->listSubscriptions('id', array('mine' => 'true', 'forChannelId' => $channel_id, 'maxResults' => 1));
             if ($is_user_subscribed['items']) {
                 $unsubscribe_id = $is_user_subscribed['items'][0]['id'];
             }
             echo $unsubscribe_id;
         }
     } catch (Google_ServiceException $e) {
         echo '<span id="response_message" class="yt4wp-error-alert"><strong>Oh No!</strong> We have encountered a serious error : <code> ' . $e->getMessage() . '</code>' . $e->getCode();
         /* Write the error to our error log */
         $this->writeErrorToErrorLog($e->getMessage(), $e->getCode());
     } catch (Google_Exception $e) {
         echo '<span id="response_message" class="yt4wp-error-alert"><strong>Oh No!</strong> We have encountered a serious error : <code> ' . $e->getMessage() . '</code>' . $e->getCode();
         /* Write the error to our error log */
         $this->writeErrorToErrorLog($e->getMessage(), $e->getCode());
     }
 }
        die('The session state did not match.');
    }
    $client->authenticate($_GET['code']);
    $_SESSION['token'] = $client->getAccessToken();
    header('Location: ' . $redirect);
}
if (isset($_SESSION['token'])) {
    $client->setAccessToken($_SESSION['token']);
}
// Check to ensure that the access token was successfully acquired.
if ($client->getAccessToken()) {
    try {
        // This code subscribes the authenticated user to the specified channel.
        // Identify the resource being subscribed to by specifying its channel ID
        // and kind.
        $resourceId = new Google_Service_YouTube_ResourceId();
        $resourceId->setChannelId('UCtVd0c0tGXuTSbU5d8cSBUg');
        $resourceId->setKind('youtube#channel');
        // Create a snippet object and set its resource ID.
        $subscriptionSnippet = new Google_Service_YouTube_SubscriptionSnippet();
        $subscriptionSnippet->setResourceId($resourceId);
        // Create a subscription request that contains the snippet object.
        $subscription = new Google_Service_YouTube_Subscription();
        $subscription->setSnippet($subscriptionSnippet);
        // Execute the request and return an object containing information
        // about the new subscription.
        $subscriptionResponse = $youtube->subscriptions->insert('id,snippet', $subscription, array());
        $htmlBody .= "<h3>Subscription</h3><ul>";
        $htmlBody .= sprintf('<li>%s (%s)</li>', $subscriptionResponse['snippet']['title'], $subscriptionResponse['id']);
        $htmlBody .= '</ul>';
    } catch (Google_Service_Exception $e) {
 while (!$status && !feof($handle)) {
     $chunk = fread($handle, $chunkSizeBytes);
     $status = $media->nextChunk($chunk);
 }
 fclose($handle);
 // If you want to make other calls after the file upload, set setDefer back to false
 $client->setDefer(false);
 if ($screen_base != 'toplevel_page_youtube-for-wordpress') {
     $insert_button = '<a class="button button-secondary insert_video_button">insert</a>';
 } else {
     $insert_button = '';
 }
 // if the user selected to upload to a playlist
 // else we just skip it
 if ($playlist_id != '') {
     $resourceId = new Google_Service_YouTube_ResourceId();
     $resourceId->setVideoId($status['id']);
     $resourceId->setKind('youtube#video');
     $playlistItemSnippet = new Google_Service_YouTube_PlaylistItemSnippet();
     $playlistItemSnippet->setTitle($_POST['video-title']);
     $playlistItemSnippet->setPlaylistId($playlist_id);
     $playlistItemSnippet->setResourceId($resourceId);
     $playlistItem = new Google_Service_YouTube_PlaylistItem();
     $playlistItem->setSnippet($playlistItemSnippet);
     $insertToPlaylistRequest = $youtube->playlistItems->insert('snippet,contentDetails', $playlistItem, array());
 }
 $htmlBody .= '<h3>Content Successfuly Uploaded</h3><p>Your new content should now be viewable from within the <a style="margin-top:-6px;" class="button-secondary" href="?page=youtube-for-wordpress&amp;tab=youtube_plus_browse">browse</a> tab, but may be unavailable until it completes processing.</p><br /><a href="#" class="upload_another_video button-secondary">Upload Another</a>';
 // print_r($status);
 $htmlBody .= '</ul>';
 // testing returned data from
 // YouTube API