Last active
August 29, 2015 14:03
-
-
Save tszym/eba3c86eac18e44ae0f3 to your computer and use it in GitHub Desktop.
Youtube Data API v3 - Get my uploaded videos
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 | |
// ... | |
if ($client->getAccessToken()) { | |
// Create analytics service object. | |
$youtube = new Google_Service_YouTube($client); | |
try { | |
// Call the channels.list method to retrieve information about the | |
// currently authenticated user's channel. | |
$channelsResponse = $youtube->channels->listChannels('contentDetails', array( | |
'mine' => 'true', | |
)); | |
$htmlBody = ''; | |
foreach ($channelsResponse['items'] as $channel) { | |
// Extract the unique playlist ID that identifies the list of videos | |
// uploaded to the channel, and then call the playlistItems.list method | |
// to retrieve that list. | |
$uploadsListId = $channel['contentDetails']['relatedPlaylists']['uploads']; | |
// Retrieve the list of videos | |
$playlistItemsResponse = $youtube->playlistItems->listPlaylistItems('snippet', array( | |
'playlistId' => $uploadsListId, | |
'maxResults' => 50 | |
)); | |
$htmlBody .= "<h3>Videos in list $uploadsListId</h3><ul>"; | |
foreach ($playlistItemsResponse['items'] as $playlistItem) { | |
$htmlBody .= sprintf('<li><a href="https://www.youtube.com/watch?v=%s">%s (%s)</a></li>', | |
$playlistItem['snippet']['resourceId']['videoId'], | |
$playlistItem['snippet']['title'], | |
$playlistItem['snippet']['resourceId']['videoId']); | |
$htmlBody .= sprintf('<img src="%s" />', $playlistItem['snippet']['thumbnails']['default']['url']); | |
$htmlBody .= sprintf('<br><p>%s</p>', $playlistItem['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 my uploaded 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