YouTube Apps脚本API只能由没有YouTube频道的帐户运行?

时间:2017-10-02 06:19:21

标签: google-apps-script google-apps

这是一个奇怪的问题,似乎没有在网上任何文件记录。

每当我尝试执行需要授权访问API的脚本时,如果我选择从GMail帐户授权代码而不是YouTube帐户,代码将仅在授权后继续执行。

在选择从YouTube帐户授权后,代码确实无法执行。除非代码仅通过GMail帐户授权,否则即使函数的第1行上的Logger.Log()调用也不会触发。

从YouTube帐户授权时,它会不断循环并永久请求授权,而不会继续。从GMail帐户授权时,代码无法正常运行,因为它正在请求YouTube数据。

问题当然是我试图从我的YouTube帐户访问Google Analytics数据,这是GMail帐户所没有的。

真的希望有人能够提供这方面的见解。我已经在多个浏览器,不同的YouTube帐户,GMails等中进行了测试,问题仍然存在。

任何请求YouTube Analytics API的代码也会发生这种情况。可以使用此处的示例代码进行复制,粘贴在下面:https://developers.google.com/youtube/analytics/v1/code_samples/apps-script#export_youtube_analytics_data_to_google_sheets

function spreadsheetAnalytics() {
  // Get the channel ID
  var myChannels = YouTube.Channels.list('id', {mine: true});
  var channel = myChannels.items[0];
  var channelId = channel.id;

  // Set the dates for our report
  var today = new Date();
  var oneMonthAgo = new Date();
  oneMonthAgo.setMonth(today.getMonth() - 1);
  var todayFormatted = Utilities.formatDate(today, 'UTC', 'yyyy-MM-dd')
  var oneMonthAgoFormatted = Utilities.formatDate(oneMonthAgo, 'UTC', 'yyyy-MM-dd');

  // The YouTubeAnalytics.Reports.query() function has four required parameters and one optional
  // parameter. The first parameter identifies the channel or content owner for which you are
  // retrieving data. The second and third parameters specify the start and end dates for the
  // report, respectively. The fourth parameter identifies the metrics that you are retrieving.
  // The fifth parameter is an object that contains any additional optional parameters
  // (dimensions, filters, sort, etc.) that you want to set.
  var analyticsResponse = YouTubeAnalytics.Reports.query(
    'channel==' + channelId,
    oneMonthAgoFormatted,
    todayFormatted,
    'views,likes,dislikes,shares',
    {
      dimensions: 'day',
      sort: '-day'
    });

  // Create a new Spreadsheet with rows and columns corresponding to our dates
  var ssName = 'YouTube channel report ' + oneMonthAgoFormatted + ' - ' + todayFormatted;
  var numRows = analyticsResponse.rows.length;
  var numCols = analyticsResponse.columnHeaders.length;

  // Add an extra row for column headers
  var ssNew = SpreadsheetApp.create(ssName, numRows + 1, numCols);

  // Get the first sheet
  var sheet = ssNew.getSheets()[0];

  // Get the range for the title columns
  // Remember, spreadsheets are 1-indexed, whereas arrays are 0-indexed
  var headersRange = sheet.getRange(1, 1, 1, numCols);
  var headers = [];

  // These column headers will correspond with the metrics requested
  // in the initial call: views, likes, dislikes, shares
  for(var i in analyticsResponse.columnHeaders) {
    var columnHeader = analyticsResponse.columnHeaders[i];
    var columnName = columnHeader.name;
    headers[i] = columnName;
  }
  // This takes a 2 dimensional array
  headersRange.setValues([headers]);

  // Bold and freeze the column names
  headersRange.setFontWeight('bold');
  sheet.setFrozenRows(1);

  // Get the data range and set the values
  var dataRange = sheet.getRange(2, 1, numRows, numCols);
  dataRange.setValues(analyticsResponse.rows);

  // Bold and freeze the dates
  var dateHeaders = sheet.getRange(1, 1, numRows, 1);
  dateHeaders.setFontWeight('bold');
  sheet.setFrozenColumns(1);

  // Include the headers in our range. The headers are used
  // to label the axes
  var range = sheet.getRange(1, 1, numRows, numCols);
  var chart = sheet.newChart()
                   .asColumnChart()
                   .setStacked()
                   .addRange(range)
                   .setPosition(4, 2, 10, 10)
                   .build();
  sheet.insertChart(chart);

}
youtube.gs

2 个答案:

答案 0 :(得分:1)

Google已将related issue ticket标记为“不会修复”。它描述了你的问题:

  

如果您尝试使用YouTube API获取有关使用Google+关联系统管理的YouTube频道的数据,则会让您陷入无限循环。 Google+信息页可以“拥有”YouTube频道,并且在尝试使用其中一个频道时授权无效

Google确实有suggested workaround

  

使用OAuth2库授权访问G +页面,然后使用UrlFetchApp而不是YouTube Advanced服务向API发出请求

由于这是一个问题,我也遇到了published a detailed tutorial that uses the YouTube Data API。由于您需要的代码也需要通过YouTube Analytics API访问,因此需要在下面详细介绍一些其他设置,并参考教程中的部分:

  1. 添加ID为1MWD64g7dq_ZhlN8HU_O6BRu5xNwywhp8V76utKowZEtcirEgO3t_JFFL(第1.4部分)
  2. 的YouTube Analytics资讯档库
  3. getYouTubeService()yt-analytics-monetary.readonly {第2部分}的yt-analytics.readonly添加其他范围

    // Set the scope and additional Google-specific parameters.
    .setScope(["https://www.googleapis.com/auth/youtube",
              "https://www.googleapis.com/auth/youtube.force-ssl",
              "https://www.googleapis.com/auth/youtube.readonly",
              "https://www.googleapis.com/auth/youtubepartner",
              "https://www.googleapis.com/auth/youtubepartner-channel-audit",
              "https://www.googleapis.com/auth/yt-analytics-monetary.readonly",
              "https://www.googleapis.com/auth/yt-analytics.readonly"])
    
  4. 启用YouTube Analytics API以及YouTube Data API(第3.4部分)

  5. 您的代码示例中还有一些小修改,以启用库自动完成功能,例如YouTube.Channels.list()变为YouTube.channelsList()YouTubeAnalytics.Reports.query()变为YouTubeAnalytics.reportsQuery()

    所有这些变化都包含在this example sheet中。您仍然需要完成本教程和auth.gs表中概述的控制台项目的所有设置(我已修改示例以在@OnlyCurrentDoc中运行,以避免应用验证)。

    注意 :当您运行logRedirectUri()时,您需要使用Google云端硬盘帐户进行身份验证,并在新的浏览器标签中复制身份验证网址时选择YouTube您想要数据的帐户。

答案 1 :(得分:0)

确保您使用的是正确的范围。要检索有关为频道或内容所有者安排的特定报告作业的信息,您必须使用以下authorization scopes

  • https://www.googleapis.com/auth/yt-analytics.readonly
    • 查看YouTube内容的YouTube Analytics报告。此范围提供对用户活动指标的访问,例如查看计数和评级计数。
  • https://www.googleapis.com/auth/yt-analytics-monetary.readonly
    • 查看YouTube内容的YouTube Analytics货币报告。此范围提供对用户活动指标以及估算收入和广告效果指标的访问权限。

查看此link以获取更多信息。

相关问题