Last active
May 20, 2021 15:48
-
-
Save chrisrasco/fc20d24473030417a155 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Call set_include_path() as needed to point to your client library. | |
require_once 'src/Google/Client.php'; | |
require_once 'src/Google/Service/YouTube.php'; | |
session_start(); | |
/* | |
* You can acquire an OAuth 2.0 client ID and client secret from the | |
* {{ Google Cloud Console }} <{{ https://cloud.google.com/console }}> | |
* For more information about using OAuth 2.0 to access Google APIs, please see: | |
* <https://developers.google.com/youtube/v3/guides/authentication> | |
* Please ensure that you have enabled the YouTube Data API for your project. | |
*/ | |
$OAUTH2_CLIENT_ID = 'YOUR ID HERE'; | |
$OAUTH2_CLIENT_SECRET = 'YOUR SECRET HERE'; | |
$client = new Google_Client(); | |
$client->setClientId($OAUTH2_CLIENT_ID); | |
$client->setClientSecret($OAUTH2_CLIENT_SECRET); | |
$client->setScopes('https://www.googleapis.com/auth/youtube'); | |
$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'], | |
FILTER_SANITIZE_URL); | |
$client->setRedirectUri($redirect); | |
// Define an object that will be used to make all API requests. | |
$youtube = new Google_Service_YouTube($client); | |
if (isset($_GET['code'])) { | |
if (strval($_SESSION['state']) !== strval($_GET['state'])) { | |
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{ | |
// REPLACE this value with the video ID of the video being updated. | |
//$videoId = "YOUR VIDEO ID HERE"; | |
$videoId = $_GET['video_id']; | |
// Call the API's videos.list method to retrieve the video resource. | |
//$listResponse = $youtube->videos->listVideos("snippet", | |
$listResponse = $youtube->videos->listVideos("contentDetails", | |
array('id' => $videoId)); | |
// If $listResponse is empty, the specified video was not found. | |
if (empty($listResponse)) { | |
$htmlBody .= sprintf('<h3>Can\'t find a video with video id: %s</h3>', $videoId); | |
} else { | |
// Since the request specified a video ID, the response only | |
// contains one video resource. | |
$video = $listResponse[0]; | |
$duration_string = $video['contentDetails']['duration']; | |
$pattern = '/PT(([0-9]+)M)?(([0-9]+)S)?/'; | |
preg_match($pattern, $duration_string, $matches); | |
if(!empty($matches[2])) | |
{ | |
$mins = $matches[2]; | |
} | |
else | |
{ | |
$mins = 0; | |
} | |
if(!empty($matches[4])) | |
{ | |
$secs = $matches[4]; | |
} | |
else | |
{ | |
$secs = 0; | |
} | |
$htmlBody .= "<h3>Video Duration</h3>"; | |
$htmlBody .= "<p>".$mins." mins</p>"; | |
$htmlBody .= "<p>".$secs." secs</p>"; | |
} | |
} 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(); | |
} else { | |
// If the user hasn't authorized the app, initiate the OAuth flow | |
$state = mt_rand(); | |
$client->setState($state); | |
$_SESSION['state'] = $state; | |
$authUrl = $client->createAuthUrl(); | |
$htmlBody = <<<END | |
<h3>Authorization Required</h3> | |
<p>You need to <a href="$authUrl">authorize access</a> before proceeding.<p> | |
END; | |
} | |
?> | |
<!doctype html> | |
<html> | |
<head> | |
<title>Video Duration</title> | |
</head> | |
<body> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment