YouTube数据APi可以使用Google App脚本在YouTube品牌帐户上检索视频

时间:2018-11-24 02:57:01

标签: google-apps-script youtube-api

我希望能够获得品牌帐户中每个视频的分析报告。.我正在该网站上的一个教程中链接:https://mashe.hawksey.info/2017/10/getting-youtube-analytics-with-google-apps-script-when-you-get-authentication-loops/在该教程中,我提供了一种获取和存储授权令牌的方法使用“ cGoa”库。我的方法是首先获取YoutUbe帐户中的所有视频并将其存储到一个数组中,然后为每个带有循环的视频获取Analytics(分析)。但是我在运行代码从频道检索视频时遇到错误。以下是我想用来检索视频的代码。注意:我正在使用我从上面的页面链接中阅读的教程中提供的YouTube库

var goa = cGoa.GoaApp.createGoa('youtube-analytics',
                                  PropertiesService.getUserProperties())
                                  .execute();
   
  YouTube.setTokenService(function(){ return goa.getToken(); } );
  //YouTubeAnalytics.setTokenService(function(){ return goa.getToken(); });  
 
  var results = YouTube.channelsList('id,snippet,contentDetails,statistics', {mine: true});
  //Logger.log(results[0].contentDetails.relatedPlaylists.uploads);
  //Logger.log(results[0].id);
  var channelId = results[0].id;
  for(var i in results) {
    var item = results[i];
    // Get the playlist ID, which is 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 = '';
    var titleArray = [];
    var idArray = [];
    var reportArray = [];
    
    // This loop retrieves a set of playlist items and checks the nextPageToken in the
    // response to determine whether the list contains additional items. It repeats that process
    // until it has retrieved all of the items in the list.
    while (nextPageToken != null) {
      var playlistResponse = YouTube.playlistItemsList('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);
        titleArray[j] = playlistItem.snippet.title;
        idArray[j] = playlistItem.snippet.resourceId.videoId;
        var videoId = playlistItem.snippet.resourceId.videoId;
        reportArray[j] = runYoutubeAnalyticsReport(channelId , videoId)

      }
      //Browser.msgBox("hellon");
      nextPageToken = playlistResponse.nextPageToken;
    }
    create_cheet(idArray , titleArray , reportArray);
  }
}

运行代码时,这是我得到的错误

Error: { "error": { "errors": [ { "domain": "youtube.part", "reason": "unknownPart", "message": "snippet?pageToken=CBkQAA", "locationType": "parameter", "location": "part" } ], "code": 400, "message": "snippet?pageToken=CBkQAA" } } (line 83, file "Youtube", project "YouTube")
我已经在这个问题上奋斗了一个多星期。.希望我能解决这个问题。.我非常感谢您的帮助和响应。谢谢!

0 个答案:

没有答案