1

YouTube.Search.list in Google Apps Scripts shows this error: ReferenceError: "YouTube" is not defined (line 22).

Dashboard shows that the request went through each time I ran the code. Youtube Data API is enabled in Apps Script and Dev Console.

Any help appreciated on why I'm getting this error.

/*
  YouTube RSS Feeds
  Written by @user1535152 http://stackoverflow.com/q/30486682/512127
  Based on http://www.labnol.org/internet/twitter-rss-feed/28149/  
*/

function doGet(e) {

  var title  = ("Youtube RSS Feed for " + e.parameter.search),
      timez  = Session.getScriptTimeZone(),
      search = encodeURIComponent(e.parameter.search),
      link   = ("https://www.youtube.com/results?search_query=" + search),
      self   = ScriptApp.getService().getUrl() + "?" + search;

  var rss='<?xml version="1.0"?>';
  rss+='<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">';
  rss+='<channel><title>'+title+'</title>';
  rss+='<link>'+link+'</link>';
  rss+='<atom:link href="'+self+'" rel="self" type="application/rss+xml" />';
  rss+='<description>' + title + ' updated on ' + new Date() + '.</description>';

  var results = YouTube.Search.list('id, snippet', {
    q: search,
    maxResults: 50,
    order: 'date'
  });

  for (var i = 0; i < results.items.length; i++){
    var item = results.items[i];
    rss += "<item>";
    rss += "<title>" + item.snippet.title + "</title>";
    rss += "<link>http://www.youtube.com/watch?v=" + item.id.videoId + "</link>";
    rss += "<description>" + item.snippet.description + "</description>";
    rss += "<pubDate>" + Utilities.formatDate(new Date(item.snippet.publishedAt), timez, "EEE, dd MMM yyyy HH:mm:ss Z") + "</pubDate>";
    rss += "<guid>http://www.youtube.com/watch?v=" + item.id.videoId + "</guid>";
    rss += "</item>";
  }

  rss+="</channel></rss>";

  return ContentService.createTextOutput(rss).setMimeType(ContentService.MimeType.RSS);
}

1 Answers1

0

From your question, it was found that "Youtube Data API is enabled in Apps Script and Dev Console.". But an error of ReferenceError: "YouTube" is not defined occurs, when the script is run. So please confirm a following setting.

In order to use YouTube.Search.list(), it requires not only enabling API at API console, but also enabling at Advanced Google Services. I confirmed that when YouTube Data API is OFF at Advanced Google Services, the same error occurs. In order to enable YouTube Data API at Advanced Google Services, please confirm as follows.

  1. Open script editor that there is the script.
  2. Click Resources -> Advanced Google Services.
  3. Turn on YouTube Data API.
  4. Click OK.

If YouTube Data API is "off", please turn on by clicking. After this, please try to run, again.

If this is not useful for you, I'm sorry.

Tanaike
  • 181,128
  • 11
  • 97
  • 165