I am working on one project which requirs to lists all videos from a channel using youtubedata api 3.0 but not from gdata(feed), the Api returns only 50 videos from channel, and there is no reference to get more videos on developers.google. Help. This is my code


require_once 'contrib/Google_YoutubeService.php';*/
require_once '../upload/google-api-php-client/src/Google_Client.php';
    require_once '../upload/google-api-php-client/src/contrib/Google_YouTubeService.php';

/* You can acquire an OAuth 2 ID/secret pair from the API Access tab on the Google APIs Console
For more information about using OAuth2 to access Google APIs, please visit:
Please ensure that you have enabled the YouTube Data API for your project. */
$OAUTH2_CLIENT_ID = 'sadsadsadasdsad';
$OAUTH2_CLIENT_SECRET = 'sadsadsadasdasdasdasd';

$client = new Google_Client();
$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'],

$youtube = new Google_YoutubeService($client);

if (isset($_GET['code'])) {
  if (strval($_SESSION['state']) !== strval($_GET['state'])) {
    die('The session state did not match.');

  $_SESSION['token'] = $client->getAccessToken();
  header('Location: ' . $redirect);

if (isset($_SESSION['token'])) {

if ($client->getAccessToken()) {
  try {
    $channelsResponse = $youtube->channels->listChannels('contentDetails', array(
      'mine' => 'true',

    $htmlBody = '';
    foreach ($channelsResponse['items'] as $channel) {
      $uploadsListId = $channel['contentDetails']['relatedPlaylists']['uploads'];
      $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>%s (%s)</li>', $playlistItem['snippet']['title'],
      $htmlBody .= '</ul>';
  } catch (Google_ServiceException $e) {
    $htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>',
  } catch (Google_Exception $e) {
    $htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>',

  $_SESSION['token'] = $client->getAccessToken();
} else {
  $state = mt_rand();
  $_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>

<!doctype html>
    <title>YouTube Search</title>

Yepp it's really late but since you did not accept the answer, let me give you the solution here.

Say you hit a url BaseURL

So when you hit the api to get first 50 videos, it gives you a nextPageToken

To get the next fifty videos, you just need to append this token to your BaseURL, something like this:

BaseURL + "&pageToken=" +nextPageToken

Similarly if you have already done some pagination, you may also get a prevPageToken, now you would just append this token like explained above to get to the previous page.

  • 9
Reply Report

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions