$client->authenticate();
    $_SESSION['token'] = $client->getAccessToken();
    header('Location: ' . $redirect);
}
if (isset($_SESSION['token'])) {
    $client->setAccessToken($_SESSION['token']);
}
// Check if access token successfully acquired
if ($client->getAccessToken()) {
    try {
        // This code will create a new, private playlist in the authorized user's channel and
        // add the video to the playlist.
        // 1. Create the PlaylistSnippet and add the required data.
        $playlistSnippet = new Google_PlaylistSnippet();
        $playlistSnippet->setTitle('Test Playlist  ' . date("Y-m-d H:i:s"));
        $playlistSnippet->setDescription('A private playlist created with the YouTube API v3');
        // 2. Create playlist status describing the privacy setting.
        $playlistStatus = new Google_PlaylistStatus();
        $playlistStatus->setPrivacyStatus('private');
        // 3. Create a playlist insert request with snippet and status.
        $youTubePlaylist = new Google_Playlist();
        $youTubePlaylist->setSnippet($playlistSnippet);
        $youTubePlaylist->setStatus($playlistStatus);
        // 4. Execute the request and return an object containing information about the new playlist
        $playlistResponse = $youtube->playlists->insert('snippet,status', $youTubePlaylist, array());
        // 5. Add the video to the playlist
        //   a. Create a resource id with video id and kind.
        $resourceId = new Google_ResourceId();
        $resourceId->setVideoId('SZj6rAYkYOg');
        $resourceId->setKind('youtube#video');
        //   b. Create a snippet with resource id.