Building on YouTube APIs in the cloud with Google Apps Script

There’s nothing better in this world than a great pairing, like coffee and donuts or bees and honey. Today there’s a new one to add to the list: Google Apps Script now has built in support for the YouTube Data v3 and YouTube Analytics APIs. If you haven’t yet heard of Google Apps Script, you’re missing out on an easy-to-use tool for integrating and automating common tasks across many of Google’s services. With less than half a screen’s worth of code in Google Apps Script’s cloud-based editor, you can:

  • Dynamically update a spreadsheet containing watch-time statistics for all of your channel's videos, with all the flexibility and power of Google Sheets to sort and slice that data
  • Create a live dashboard or scheduled email report about your channel's performance
  • Handle channel management tasks such as scheduling automatic bulletins or changing the visibility of a large number of videos from private to public
  • Automate playlist rotation without having to maintain a server or keep a computer for the sole purpose of running a script
Google Apps Script's cloud-based environment and autocomplete functionality make it easy to just open an editor, enable the YouTube APIs, and start writing code:



For functions that require OAuth 2.0 authorization, there’s no authorization code to write and no token management to deal with. Once your script is ready, just click “Run” and Google Apps Script will present you with an authorization dialog. Once you select the channel you want to authorize, the script will have all of the permissions it needs to operate on your behalf, running in the background at scheduled intervals if you so desire.

To get started, browse to Google Drive. Click “Create” and then choose “Script”. This will open a new browser tab to the Google Apps Script editor. Name your project and click on “Resources” and select “Advanced Google Services”:


Toggle the YouTube Data API and/or YouTube Analytics API on:


Note the message that these services must be enabled in the API console. Click the link to be taken to the Google Developer Console. The link in the message will take you to a specific API project created for this specific Apps Script. Scroll down and toggle on the YouTube APIs the script will use:


Switch back to the tab containing Google Apps Script and click “OK”. You’re ready to start writing code. Type “YouTube” and hit the period key (“.”). If the APIs have been turned on correctly, you will be able to start writing code and calling functions available in the Data API or Analytics API. For instance, a short script that searches for videos about “dogs” and prints the video IDs to the Google Apps Script log would look like this:


function searchByKeyword() {
var results = YouTube.Search.list("id,snippet",
{q : "google apps script", maxResults: 25});

for(var i in results.items) {
var item = results.items[i];
Logger.log("[%s] Title: %s", item.id.videoId, item.snippet.title);
}
}

For more information about this update to Google Apps Script, check out the post on their official blog. To learn more about how to integrate scheduled jobs, write to spreadsheets or any of the many things Google Apps Script can do, check out the tutorials at their home at developers.google.com/apps-script. If you’re the type that prefers to learn on the go, get started with your own copy of our sample code in your Google Drive. You will still need to enable the APIs, so don’t forget to go to “Resources > Advanced Google services” for the link to the Developer Console project to turn on the APIs.


If you have any questions, feel free to find us on StackOverflow under the youtube-api and google-apps-script tags. Happy coding!


- Ikai Lan
YouTube Developer Relations