die('The session state did not match.');
    }
    $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 {
        // REPLACE with the path to your file that you want to upload
        $videoPath = "/path/to/file.mp4";
        // Create a snipet with title, description, tags and category id
        $snippet = new Google_VideoSnippet();
        $snippet->setTitle("Test title");
        $snippet->setDescription("Test description");
        $snippet->setTags(array("tag1", "tag2"));
        // Numeric video category. See
        // https://developers.google.com/youtube/v3/docs/videoCategories/list
        $snippet->setCategoryId("22");
        // Create a video status with privacy status. Options are "public", "private" and "unlisted".
        $status = new Google_VideoStatus();
        $status->privacyStatus = "public";
        // Create a YouTube video with snippet and status
        $video = new Google_Video();
        $video->setSnippet($snippet);
        $video->setStatus($status);
        // Size of each chunk of data in bytes. Setting it higher leads faster upload (less chunks,
        // for reliable connections). Setting it lower leads better recovery (fine-grained chunks)
         $htmlBody .= sprintf('<h3>Can\'t find a video with video id: %s</h3>', $videoId);
     } else {
         // Since a unique video id is given, it will only return 1 video.
         $video = $videoList[0];
         $videoSnippet = $video['snippet'];
         $tags = $videoSnippet['tags'];
         // $tags is null if the video didn't have any tags, so we will check for this and
         // create a new list if needed
         if (is_null($tags)) {
             $tags = array("tag1", "tag2");
         } else {
             array_push($tags, "tag1", "tag2");
         }
         // Construct the Google_Video with the updated tags, hence the snippet
         $updateVideo = new Google_Video($video);
         $updateSnippet = new Google_VideoSnippet($videoSnippet);
         $updateSnippet->setTags($tags);
         $updateVideo->setSnippet($updateSnippet);
         // Create a video update request
         $updateResponse = $youtube->videos->update("snippet", $updateVideo);
         $responseTags = $updateResponse['snippet']['tags'];
         $htmlBody .= "<h3>Video Updated</h3><ul>";
         $htmlBody .= sprintf('<li>Tags "%s" and "%s" added for video %s (%s) </li>', array_pop($responseTags), array_pop($responseTags), $videoId, $updateResponse['snippet']['title']);
         $htmlBody .= '</ul>';
     }
 } catch (Google_ServiceException $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();