Skip to content

Instantly share code, notes, and snippets.

@tszym
Created July 7, 2014 08:53
Show Gist options
  • Save tszym/e5232c125515fb5604e0 to your computer and use it in GitHub Desktop.
Save tszym/e5232c125515fb5604e0 to your computer and use it in GitHub Desktop.
Youtube Data API v3 - Get my favorites videos
<?php
// ...
if ($client->getAccessToken()) {
// Create analytics service object.
$youtube = new Google_Service_YouTube($client);
/**
* Retrive favourites videos of the user
*
* This is what the API explorer displays:
*
* GET https://www.googleapis.com/youtube/v3/videos?part=snippet&myRating=like&key={YOUR_API_KEY}
* Authorization: Bearer ya29.MQBbHGxzlOKL4RkAAAA6y91i1SsCmn9QV3RDij5NseeA8w2-meq7u-F2lIzHdw
* X-JavaScript-User-Agent: Google APIs Explorer
*/
try {
$videosResponse = $youtube->videos->listVideos('snippet', array(
'myRating' => 'like',
'maxResults' => 50
));
$htmlBody .= "<h3>Favorite videos</h3><ul>";
foreach ($videosResponse['items'] as $video) {
$htmlBody .= sprintf('<li><a href="https://www.youtube.com/watch?v=%s">%s (par %s)</a></li>',
$video->id,
$video['snippet']['title'],
$video['snippet']['channelTitle']);
$htmlBody .= sprintf('<img src="%s" />', $video['snippet']['thumbnails']['default']['url']);
$htmlBody .= sprintf('<br><p>%s</p>', $video['snippet']->description);
}
$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();
} else {
// ...
}
?>
<!doctype html>
<html>
<head>
<title>Youtube favorite videos</title>
<meta charset="UTF-8" />
</head>
<body>
<?php echo $htmlBody; ?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment