How to get the youtube favorites url

2019-09-03 05:04发布

问题:

I made the following

var NAME = 'youtube';
var SCOPE = 'http://gdata.youtube.com';
//var URL = "https://picasaweb.google.com/data/feed/api/user/default";
var URL = "http://gdata.youtube.com/feeds/api/users/default/favorites?v=2";

function doGet(e) {
  var app = UiApp.createApplication().setTitle("youtube");
  var data = UrlFetchApp.fetch(URL, googleOAuth_()).getContentText(); 
  var xmlOutput = Xml.parse(data, false);  
  var favorites = xmlOutput.getElement().getElements('entry');  

  app.add(app.createLabel(favorites.length.toString()))
  for(var i = 0; i < favorites.length; i++){
  app.add(app.createLabel(favorites[i].getElement('title').getText()))
  //var testf = favorites[i].getElement('http://gdata.youtube.com/schemas/2007#favorite','href');
}

return app;
}   

function googleOAuth_() {
var oAuthConfig = UrlFetchApp.addOAuthService(NAME);
 oAuthConfig.setRequestTokenUrl('https://www.google.com/accounts/OAuthGetRequestToken?scope='+SCOPE);
 oAuthConfig.setAuthorizationUrl('https://www.google.com/accounts/OAuthAuthorizeToken');
 oAuthConfig.setAccessTokenUrl('https://www.google.com/accounts/OAuthGetAccessToken');
 oAuthConfig.setConsumerKey('anonymous');
 oAuthConfig.setConsumerSecret('anonymous');
 return {oAuthServiceName:NAME, oAuthUseToken:'always'};
}

To get the title of the favorite videos it works fine
But I can´t find the way how to get to the url of the favorite video.
where can I find that in the documentation?

And than another question, when I try to execute the script
with another google account it still gives the error:
Authorization is required to perform that action

Isn´t the googleOAuth_() funcion taking care of that?

回答1:

Here is a working solution...

(EDIT : better code)

var NAME = 'youtube';
var SCOPE = 'http://gdata.youtube.com';
//var URL = "https://picasaweb.google.com/data/feed/api/user/default";
var URL = "http://gdata.youtube.com/feeds/api/users/default/favorites?v=2";

function doGet(e) {
    var app = UiApp.createApplication().setTitle("youtube").setStyleAttribute('padding','20');
    var data = UrlFetchApp.fetch(URL, googleOAuth_()).getContentText(); 
    var xmlOutput = Xml.parse(data, true);  
    var favorites = xmlOutput.getElement().getElements('entry');  
    app.add(app.createLabel('YouTube favourites entries : '+favorites.length.toString()).setStyleAttribute('padding','10'))
    var table = app.createFlexTable().setWidth('400').setStyleAttribute('background', '#ffffdd')

  for(var i = 0; i < favorites.length; i++){

      if(favorites[i].getElement('link')){  
      var url = favorites[i].getElement('link').getAttribute('href').getValue();}

      if(favorites[i].getElement('title')){
       var Title = favorites[i].getElement('title').getText()}

      table.setText(i, 0, 'Clip Title : '+Title).setWidget(i, 1, app.createAnchor('link', url)).setBorderWidth(1)  
    }
    app.add(table)
 return app;
}