YouTube Service

The YouTube service allows you to use the YouTube Data API and YouTube Live Streaming API in Apps Script. This API gives users the ability to manage their videos, playlists, channels, and live events.

Reference

For detailed information on this service, see the following reference documentation:

Like all advanced services in Apps Script, the YouTube service uses the same objects, methods, and parameters as the public API.

Sample code

The sample code below uses version 3 of the YouTube Data API.

Open code in new window

Search by keyword

This function searches for videos about dogs, then logs the video IDs and title. Note that this sample limits the results to 25. To return more results, pass additional parameters as shown in the YouTube Data API reference documentation.

function searchByKeyword() {
  var results = YouTube.Search.list('id,snippet', {
    q: 'dogs',
    maxResults: 25
  });

  for (var i = 0; i < results.items.length; i++) {
    var item = results.items[i];
    Logger.log('[%s] Title: %s', item.id.videoId, item.snippet.title);
  }
}

Retrieve uploads

This function retrieves the user's uploaded videos. It does this using the following steps:

  1. Fetches the user's channel
  2. Fetches the user's uploads playlist
  3. Iterates through this playlist and logs the video IDs and titles
  4. If there is a next page of results, fetches it, then returns to step 3
function retrieveMyUploads() {
  var results = YouTube.Channels.list('contentDetails', {
    mine: true
  });

  for (var i = 0; i < results.items.length; i++) {
    var item = results.items[i];
    // Get the channel ID - it's nested in contentDetails, as described in the
    // Channel resource:
    //    https://developers.google.com/youtube/v3/docs/channels
    var playlistId = item.contentDetails.relatedPlaylists.uploads;

    var nextPageToken;
    while (nextPageToken != null) {
      var playlistResponse = YouTube.PlaylistItems.list('snippet', {
        playlistId: playlistId,
        maxResults: 25,
        pageToken: nextPageToken
      });

      for (var j = 0; j < playlistResponse.items.length; j++) {
        var playlistItem = playlistResponse.items[j];
        Logger.log('[%s] Title: %s',
                   playlistItem.snippet.resourceId.videoId,
                   playlistItem.snippet.title);

      }
      nextPageToken = playlistResponse.nextPageToken;
    }
  }
}

Subscribe to channel

This sample subscribes the user to the Google Developers channel on YouTube.

function addSubscription() {
  // Replace this channel ID with the channel ID you want to subscribe to
  var channelId = 'UC9gFih9rw0zNCK3ZtoKQQyA';

  var resource = {
    snippet: {
      resourceId: {
        kind: 'youtube#channel',
        channelId: channelId
      }
    }
  };

  try {
    var response = YouTube.Subscriptions.insert(resource, 'snippet');
    Logger.log(response);
  } catch (e) {
    if (e.message.match('subscriptionDuplicate')) {
      Logger.log('Cannot subscribe; already subscribed to channel: ' +
          channelId);
    } else {
      Logger.log('Error adding subscription: ' + e.message);
    }
  }
}

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.