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:
![](https://web.archive.org./web/20181123192916im_/https://lh3.googleusercontent.com/iHq3Z1P37is7ya1kuuAxLYmnKfp_bilAFpleWoeSnqbv1gZpNhcaaGDhJJ_7Qs3Exc6AmcanAY8r5AOV58v38UZBzQvozstTb65t7vzKEBJv8HWhG66lHTQOQw)
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”:![](https://web.archive.org./web/20181123192916im_/https://lh3.googleusercontent.com/F_MdAYOGBImwVnHyC24sNfrmWetPpfA4IajGM0lXJBJGWwLP3ZDHMvXo0zjfOdaTJ22YwA0JPFTUmJQMUCpX8MFMvxg0W4WhP8HULohIp4zHAb1Gl4pnW8kgTA)
Toggle the YouTube Data API and/or YouTube Analytics API on:
![](https://web.archive.org./web/20181123192916im_/https://lh6.googleusercontent.com/t3yYr9LRMKmQH8y-nBZL5gsWNVmqw3AQJZ5lkjvyHQ9OCl3qS4WnImA4juF5ddWndQk4fykJnkmQqvsRShfIW5iNYBAKUpDYzkyDTK82qUmIxJnzJ_o93Act0Q)
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: ![](https://web.archive.org./web/20181123192916im_/https://lh4.googleusercontent.com/xUPPSvyhUn-pEcM0WSBFA3zZZ5TXTKi588jdRBO0mjhybYsJ1r39w0CiPhhmMUJbNkAVn15BxhpX-Wq0jTCLvzhNJ1tAs0p2_OcEdJuhmFuA57UBhJKGL4E2QQ)
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.