$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);
        $playlistItemResponse = $youtube->playlistItems->insert('snippet,contentDetails', $playlistItem, array());
        $htmlBody .= "<h3>New Playlist</h3><ul>";
        $htmlBody .= sprintf('<li>%s (%s)</li>', $playlistResponse['snippet']['title'], $playlistResponse['id']);
        $htmlBody .= '</ul>';
        $htmlBody .= "<h3>New PlaylistItem</h3><ul>";
        $htmlBody .= sprintf('<li>%s (%s)</li>', $playlistItemResponse['snippet']['title'], $playlistItemResponse['id']);
        $htmlBody .= '</ul>';
    } catch (Google_Service_Exception $e) {
        $htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>', htmlspecialchars($e->getMessage()));
    } catch (Google_Exception $e) {
        $htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>', htmlspecialchars($e->getMessage()));
    }
    $_SESSION['token'] = $client->getAccessToken();
} else {
    // If the user hasn't authorized the app, initiate the OAuth flow
 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);
     }
 }
 function addVideoToWatchLaterList($watch_later_list_id, $video_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->setVideoId($video_id);
         $resourceId->setKind('youtube#video');
         $playlistItemSnippet = new Google_Service_YouTube_PlaylistItemSnippet();
         $playlistItemSnippet->setPlaylistId($watch_later_list_id);
         $playlistItemSnippet->setResourceId($resourceId);
         $playlistItem = new Google_Service_YouTube_PlaylistItem();
         $playlistItem->setSnippet($playlistItemSnippet);
         $insertVideoToWatchLaterPlaylist = $youtube->playlistItems->insert('snippet,contentDetails', $playlistItem, array());
         if ($insertVideoToWatchLaterPlaylist) {
             echo 'Success : Video added to Watch Later playlist';
         }
     } catch (Google_ServiceException $e) {
         echo $e->getMessage();
     } catch (Google_Exception $e) {
         if (strpos($e->getMessage(), '(403) Playlist contains maximum number of items.') !== false) {
             echo 'Error : Video already exists in Watch Later playlist';
         }
     }
 }